2017-01-09 72 views
0

我在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() 

回答

1

我認爲執行方法期待一個元組或字典。 如果您更改到呼叫從

cur.execute(query, data) 

insertAndUpdateDatabase執行

cur.execute(query, (data,)) 

您的代碼應工作。

0

爲了管理數據庫,我使用了瓶擴展名:flask_sqlalchemy,它使插入,修改和刪除數據庫中的記錄成爲非常重要的工作。 例如:

給你一張表老師在一個模型模塊中定義一個名稱和年齡作爲列。要刪除記錄,你只需要做下一個:

to_delete = Teacher.query.filter_by(name="<the-one-you-want-delete>").first() 
db.session.delete(to_delete) 
db.session.commit() 

而這將是所有的, db是SQLAlchemy的實例,你可以這樣創建:

from flask_sqlalchemy import SQLAlchemy 
db = SQLAlchemy(app) 

更多細節我推薦Miguel Grinberg的書「Flask Web Development」

相關問題