gpt4 book ai didi

python - sqlite3.警告: You can only execute one statement at a time

转载 作者:IT王子 更新时间:2023-10-29 06:20:59 33 4
gpt4 key购买 nike

运行这段代码时出现错误:

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

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

关于python - sqlite3.警告: You can only execute one statement at a time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513854/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com