从数据库Flask / Sqlite

时间:2017-01-09 14:42:59

标签: python flask sqlite

我在python / flask中使用一个函数删除我数据库中的一些记录。 我唯一的问题是我只能删除id为1到9的记录。 如果我尝试删除id大于9的记录,我会收到错误:

  

ProgrammingError:提供的绑定数量不正确。当前语句使用1,并且提供了2个。

Flask代码:

@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()

2 个答案:

答案 0 :(得分:1)

我认为 execute 方法需要一个元组或字典。 如果您从 insertAndUpdateDatabase 中更改对执行的调用

cur.execute(query, data)

cur.execute(query, (data,))

您的代码应该有效。

答案 1 :(得分:0)

为了管理数据库,我使用了flask扩展名: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)

有关详细信息,我建议使用本书&#34; Flask Web Development&#34; Miguel Grinberg的作品