python – sqlite3.Warning:一次只能执行一个语句

运行此代码时出现错误:

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)

为什么会这样?

虽然其他海报对于您的语句格式是正确的,但您收到此特定错误,因为您尝试在一个查询中执行多个语句(请注意查询中的;分隔语句).

从Python sqlite3文档:

“execute() will only execute a single SQL statement. If you try to execute more than one  statement with it, it will raise a Warning. Use executescript() if you want to execute  multiple SQL statements with one call.”

https://docs.python.org/2/library/sqlite3.html

现在,即使您使用了executioncript(),您的语句也无法正确执行,因为格式化方式存在其他问题(请参阅其他已发布的答案).但是您收到的错误特别是因为您的多个陈述.我正在为搜索该错误后可能在这里闲逛的其他人发布此答案.

翻译自:https://stackoverflow.com/questions/15513854/sqlite3-warning-you-can-only-execute-one-statement-at-a-time