SQLite:编辑帐户

wbgh16ku  于 2022-11-30  发布在  SQLite
关注(0)|答案(1)|浏览(137)

我正在尝试根据用户ID编辑用户帐户。我可以从数据库中检索用户数据,但在单击“保存”按钮后尝试将数据更新到数据库中时出现问题。我不知道为什么会出现问题。请问我哪里出错了?

www.example.com中的sql语句 app.py

@app.route('/edit',methods = ['GET','POST'])
def edit():
    id = session['id']
    conn = get_db_connection()
    users = conn.execute('SELECT * FROM User WHERE id = ?',(id)).fetchall()

    form = EditForm(request.form)
    if request.method == 'POST' and form.validate():
        hashed_pw = generate_password_hash(form.password.data,"sha256")
        print(form.first_name.data, form.last_name.data, form.email.data, hashed_pw,  id)
        conn.execute('UPDATE User SET first_name = ?,last_name = ?,email = ?, password = ? WHERE id = ?',(form.first_name.data, form.last_name.data, form.email.data, hashed_pw,  id))
        conn.commit()
        conn.close()
        message = "Data has been modify successfully"
        flash(message,'edited')
        return redirect(url_for('edit'))
    return render_template('edit.html',users = users, form = form)

编辑.html

<form action="/edit" method="POST">
        <div>
            <h2 class="text-center fw-bold fs-2 mb-4 p-0 m-0">Edit Account</h2>
          {% for user in users %}
            <div class="input-group justify-content-between">
                
                <div class="form-floating mb-4" style="min-width: 230px;">
                    <label for="firstName">First name</label>
                    <input id="first_name" name="first_name" class="form-control" placeholder="John" value= "{{ user[1] }}"  />
                    {% if form.first_name.errors %}
                        <ul class="errors text-danger">
                        {% for error in form.first_name.errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                        </ul>
                    {% endif %}
                </div>

                <div class="form-floating mb-4" style="min-width: 230px;">
                    <label for="lastName">Last name</label>
                    <input id="last_name" name="last_name" class="form-control" placeholder="Doe" value= "{{ user[2] }}"/>
                    {% if form.last_name.errors %}
                        <ul class="errors text-danger">
                        {% for error in form.last_name.errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                        </ul>
                    {% endif %}
                </div>

            </div>

            <div class="form-floating mb-4">
                <label for="email">Email address</label>
                <input id="email" name="email" class="form-control" type="email" placeholder="name@example.com" value= "{{ user[3] }}" />
                {% if form.email.errors %}
                    <ul class="errors text-danger">
                    {% for error in form.email.errors %}
                        <li>{{ error }}</li>
                    {% endfor %}
                    </ul>
                {% endif %}
            </div>

            <div class="form-floating mb-4">
                <label for="password">Password</label>
                <input id="password" name="password" class="form-control" type="Password" placeholder="Enter your password" />
                {% if form.password.errors %}
                    <ul class="errors text-danger">
                    {% for error in form.password.errors %}
                        <li>{{ error }}</li>
                    {% endfor %}
                    </ul>
                {% endif %}
            </div>

            <div class="form-floating mb-4">
                <label for="passwordConfirmation">Confirm password</label>
                <input id="password_confirmation" name="password_confirmation" class="form-control" type="password" placeholder="Enter your password again" />
                {% if form.password_confirmation.errors %}
                    <ul class="errors text-danger">
                    {% for error in form.password_confirmation.errors %}
                        <li>{{ error }}</li>
                    {% endfor %}
                    </ul>
                {% endif %}
            </div>
            
            {% endfor %}

[

这是处理update sql语句时出现的问题

Traceback (most recent call last):
  File "C:\Users\user\desktop\phonebuddy\.venv\lib\site-packages\flask\app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\user\desktop\phonebuddy\.venv\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\user\desktop\phonebuddy\.venv\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\user\desktop\phonebuddy\.venv\lib\site-packages\flask\app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "C:\Users\user\Desktop\PhoneBuddy\app.py", line 127, in edit
    conn.execute('UPDATE User SET first_name = ?,last_name = ?,email = ?, password = ? WHERE id = ?',(form.first_name.data, form.last_name.data, form.email.data, hashed_pw,  id))
sqlite3.InterfaceError: Error binding parameter 4 - probably unsupported type.
127.0.0.1 - - [28/Nov/2022 16:29:50] "POST /edit HTTP/1.1" 500 -

print()行的输出

mcvgt66p

mcvgt66p1#

错误消息指出参数4的类型不受支持。计数从0开始,因此我们讨论的是id。调试输出显示id包含one-item python tuple(2,)
这对于只传递一个参数的select语句来说没有问题,因为它可以同时传递单个值和元组。但是如果像update语句那样传递多个id,则会得到一个包含元组的元组。
请检查您如何设置session['id']。如果您只想传递一个ID,则不要传递元组。如果您想传递元组,则在将其传递给update语句时获取第一个值id[0]

相关问题