运行此代码时出现错误:
import sqlite3
user_name = raw_input("Please enter the name: ")
user_email = raw_input("Please enter the email: ")
db = sqlite3.connect("customer")
cursor=db.cursor()
sql = """INSERT INTO customer
(name, email) VALUES (?,?);,
(user_name, user_email)"""
cursor.execute(sql)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
小智 20
虽然其他海报对于您的语句格式是正确的,但您正在接收此特定错误,因为您尝试在一个查询中执行多个语句(请注意查询中的;分隔语句).
从Python sqlite3文档:
"execute()只会执行一个SQL语句.如果你尝试用它执行多个语句,它会引发一个警告.如果你想用一次调用执行多个SQL语句,请使用executioncript()."
https://docs.python.org/2/library/sqlite3.html
现在,即使您使用了executioncript(),您的语句也无法正确执行,因为格式化方式存在其他问题(请参阅其他已发布的答案).但是您收到的错误特别是因为您的多个语句.我正在为搜索该错误后可能在这里闲逛的其他人发布此答案.
Anu*_*ava 15
用executescript而不是execute
execute()只执行一个SQL语句.如果您尝试使用它执行多个语句,则会引发警告.如果要通过一次调用执行多个SQL语句,请使用execucript().
https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute
您;,在查询字符串的中间有一个- 这是无效的语法。execute如果要使用命名参数绑定,则将字典作为第二个参数传递给。
sql = "INSERT INTO customer (name, email) VALUES (:name, :email)"
cursor.execute(sql, {'name':user_name, 'email':user_email})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16471 次 |
| 最近记录: |