我在python/flask中使用了一個函數來刪除我的數據庫中的一些記錄。 我唯一的問題是,我只能用一個id刪除記錄從1到9 如果我嘗試刪除一條記錄與高於9的ID我得到的錯誤:從數據庫中選擇一條記錄Flask/Sqlite
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.
瓶代碼:
@app.route('/change-teacher', methods = ['GET', 'POST'])
def changeTeacher():
teacherId = request.form['id']
teachers = selectFromDatabaseWithVar("SELECT * FROM leraren WHERE id = ?", teacherId)
teacherData = [dict(id = row[0], naam = row[1], voornaam = row[2], foto = row[3], email = row[4]) for row in teachers]
return render_template("leraarAanpassen.html", teacherData = teacherData)
@app.route('/change-teacher/action', methods = ['GET', 'POST'])
def changeTeacherAction():
teacherData = (request.form['name'], request.form['firstName'], request.form['email'], request.form['id'])
insertAndUpdateDatabase("UPDATE leraren SET naam = ?, voornaam = ?, email = ? WHERE id = ?", teacherData)
return redirect(url_for("teachers"))
@app.route('/delete-teacher', methods = ['GET', 'POST'])
def deleteTeacher():
teacherId = request.form['id']
insertAndUpdateDatabase("DELETE FROM leraren WHERE id = ?", teacherId)
return redirect(url_for("teachers"))
模板:
{% include "dashboard.html" %}
{% block content %}
<table>
<tr>
<th>ID</th>
<th>NAAM</th>
<th>VOORNAAM</th>
<th>FOTO</th>
<th>EMAIL</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
{% for leraar in leraren %}
<tr>
<td>{{ leraar.id }}</td>
<td>{{ leraar.naam }}</td>
<td>{{ leraar.voornaam }}</td>
<td>{{ leraar.foto }}</td>
<td>{{ leraar.email }}</td>
<td>
<form method="POST" action="/change-teacher">
<button type="submit" name="id" value="{{ leraar.id }}">
<img src="{{ url_for('static', filename='img/settings.png') }}">
</button>
</form>
</td>
<td>
<form method="POST" action="/delete-teacher">
<button type="submit" name="id" value="{{ leraar.id }}">
<img src="{{ url_for('static', filename='img/trash.png') }}">
</button>
</form>
</td>
</tr>
{% endfor %}
</table>
<a href="/leraartoevoegen"><input type="button" name="addRecord" class="newRecord" value="Nieuwe record toevoegen"></a>
{% endblock %}
我刪除功能:
def insertAndUpdateDatabase(query, data):
db = sqlite3.connect('schooldatabase.db')
cur = db.cursor()
cur.execute(query, data)
db.commit()