Python sqlite3.Warning: 一次只能执行一条语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15513854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlite3.Warning: You can only execute one statement at a time
提问by spamup
I get the error when running this code:
运行此代码时出现错误:
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)
Why is this happening?
为什么会这样?
回答by A. Rodas
You have a ;,in the middle of the query string - that is an invalid syntax. Pass a dictionary as a second argument to executeif you want to use a named parameter binding.
您;,在查询字符串的中间有一个- 这是无效的语法。execute如果要使用命名参数绑定,则将字典作为第二个参数传递给。
sql = "INSERT INTO customer (name, email) VALUES (:name, :email)"
cursor.execute(sql, {'name':user_name, 'email':user_email})
回答by rev
While the other posters are correct about your statement formatting you are receiving this particular error because you are attempting to perform multiple statements in one query (notice the ; in your query which separates statements).
虽然其他海报关于您的语句格式是正确的,但您收到此特定错误是因为您试图在一个查询中执行多个语句(注意 ; 在您的查询中分隔语句)。
From Python sqlite3 docs:
来自 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."
“execute() 只会执行一个 SQL 语句。如果你试图用它执行多个语句,它会引发警告。如果你想一次调用执行多个 SQL 语句,请使用 executescript()。”
https://docs.python.org/2/library/sqlite3.html
https://docs.python.org/2/library/sqlite3.html
Now your statement will not execute properly even if you use executescript() because there are other issues with the way it is formatted (see other posted answers). But the error you are receiving is specifically because of your multiple statements. I am posting this answer for others that may have wandered here after searching for that error.
现在,即使您使用 executescript(),您的语句也不会正确执行,因为它的格式存在其他问题(请参阅其他已发布的答案)。但是您收到的错误是因为您的多个语句。我正在为在搜索该错误后可能在这里徘徊的其他人发布此答案。
回答by Anupam Srivastava
Use executescriptinstead of execute
使用executescript代替execute
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.
execute() 只会执行一条 SQL 语句。如果您尝试使用它执行多个语句,则会引发警告。如果您想通过一次调用执行多条 SQL 语句,请使用 executescript()。
https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute
https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute
回答by Andreas Koutroumpas
Try this:
尝试这个:
sql = """INSERT INTO customer
(name, email) VALUES (?,?)"""
cursor.execute(sql, (user_name, user_email))
回答by Omar Kamel Mostafa
import sqlite3
def DB():
List = {"Name":"Omar", "Age":"33"}
columns = ', '.join("" + str(x).replace('/', '_') + "" for x in List.keys())
values = ', '.join("'" + str(x).replace('/', '_') + "'" for x in List.values())
sql_qry = "INSERT INTO %s ( %s ) values (?,?) ; ( %s )" % ('Table Name', columns, values)
conn = sqlite3.connect("DBname.db")
curr = conn.cursor()
# curr.execute("""create table if not exists TestTable(
# Name text,
# Age text
# )""")
# print columns
# print values
# print sql
# sql = 'INSERT INTO yell (Name , Age) values (%s, %s)'
curr.execute(sql_qry)
DB()

