You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RuntimeError: near ")": syntax error错误:数据库插入语句求助

解决方案

这个错误的核心原因是你调用db.execute时的参数传递方式不对——大部分Python数据库驱动(比如sqlite3)要求SQL占位符对应的参数必须以元组/列表的形式传入,而不是拆成独立参数逐个传递。

你现在的代码把username, hash, email作为三个单独参数传给execute,驱动会错误解析SQL语句,最终导致语法报错。

修正后的代码如下:

# generate password hash and insert the user into the database
hash = generate_password_hash(password)
# 将参数打包成元组传入
db.execute("INSERT INTO users(username, hash, email) VALUES (?,?,?)", (username, hash, email))

额外验证点:

  • 确认你的hash字段在数据库中确实存在(你已经提到过,这条可以快速核对一遍)
  • 如果使用的是MySQL等数据库,注意关键字问题——hash是MySQL的保留字,这种情况下需要用反引号包裹字段名:INSERT INTO users(username, hash, email) VALUES (?,?,?)

内容的提问来源于stack exchange,提问作者Abdirashid Abubakar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 09:45:29