Я только что узнал, как использовать sqlite3 для редактирования записи из внешнего интерфейса, но у меня возникают некоторые проблемы при отладке синтаксической ошибки.
Строка cur.execute("SELECT * FROM clubs WHERE id= %s", id)
- это то, что дает ошибку. Я пробовал cur.execute("SELECT * FROM clubs WHERE id= %s", [id])
и cur.execute("SELECT * FROM clubs WHERE id= %s", [club_id])
, но ничего не решает проблему.
rout.py
@clubs.route('/user_clubs/<int:user_id>', methods=['GET', 'POST'])
@login_required
def user_clubs(user_id):
user = User.query.get_or_404(user_id)
clubs = user.clubs
form = ClubRegistrationForm()
if form.validate_on_submit():
advisor = User.query.filter_by(email=form.email.data).first()
# TODO: Change this back to `ROLES['teacher']`
if not advisor or advisor.role != ROLES['student']:
flash('That email does not belong to an advisor, or does not exist at all!', 'danger')
# TODO: should this be a redirect?
return render_template('user_clubs.html', clubs=clubs, user=user, form=form)
club = Club(name=form.club_name.data)
# TODO: ADD CHECK UNIQUE CLUB
db.session.add(club)
club.members.append(advisor)
user.clubs.append(club)
db.session.commit()
flash('Your club has been created!', 'success')
return redirect(url_for('clubs.user_clubs', user_id=user_id))
return render_template('user_clubs.html', clubs=clubs, user=user, form=form) #change to num_club_members
@clubs.route('/edit_clubs/<int:user_id>/<int:club_id>', methods=['GET', 'POST'])
@login_required
def user_clubs_edit(user_id, club_id):
user = User.query.get_or_404(user_id)
club = Club.query.get_or_404(club_id)
clubs = user.clubs
id=str(club_id)
# create cursor
cur = sqlite3.connect('site.db')
# get club by id
cur.execute("SELECT * FROM clubs WHERE id= %s", id)
club = cur.fetchone()
cur.close()
# get form
form = ClubRegistrationForm()
# populate fields
form.club_name.data=club['club_name']
form.advisor.data=club['advisor']
if form.validate_on_submit():
advisor = User.query.filter_by(email=form.email.data).first()
# TODO: Change this back to `ROLES['teacher']`
if not advisor or advisor.role != ROLES['student']:
flash('That email does not belong to an advisor, or does not exist at all!', 'danger')
# TODO: should this be a redirect?
return render_template('user_clubs.html', clubs=clubs, user=user, form=form)
club = Club(name=form.club_name.data)
# TODO: ADD CHECK UNIQUE CLUB
db.session.add(club)
club.members.append(advisor)
user.clubs.append(club)
db.session.commit()
flash('Your club has been updated!', 'success')
return redirect(url_for('clubs.user_clubs_edit', user_id=user_id))
return render_template('user_clubs_edit.html', club=club, user=user, form=form)
Я ожидаю клуб с указаннымИдентификатор для извлечения в club=cur.fetchone()
, чтобы я мог получить доступ к данным для этого клуба и ввести их в новую форму.