sqlite3.Warning: 一次只能执行一条语句 [英] sqlite3.Warning: You can only execute one statement at a time

查看:48
本文介绍了sqlite3.Warning: 一次只能执行一条语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时出现错误:

导入 sqlite3user_name = raw_input("请输入姓名:")user_email = raw_input("请输入邮箱:")db = sqlite3.connect("客户")游标=db.cursor()sql = """插入客户(姓名, 电子邮件) VALUES (?,?);,(user_name, user_email)"""游标.执行(sql)

为什么会这样?

解决方案

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

来自 Python sqlite3 文档:

<块引用>

"execute() 只会执行一个 SQL 语句.如果你尝试执行多个用它声明,它会引发警告.如果要执行,请使用 executescript()一次调用多条 SQL 语句."

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

现在,即使您使用 executescript(),您的语句也不会正确执行,因为它的格式存在其他问题(请参阅其他已发布的答案).但是您收到的错误是因为您的多个语句.我正在为在搜索该错误后可能在这里徘徊的其他人发布此答案.

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?

解决方案

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:

"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

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.

这篇关于sqlite3.Warning: 一次只能执行一条语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆