【问题标题】:sqlite3.Warning: You can only execute one statement at a timesqlite3.Warning:一次只能执行一条语句
【发布时间】:2013-03-20 01:46:21
【问题描述】:

运行此代码时出现错误:

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 开始,我记得查询和参数必须是单独的参数,而不是单个字符串。可以试试吗?

标签: python sqlite


【解决方案1】:

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

来自 Python sqlite3 文档:

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

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

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

【讨论】:

    【解决方案2】:

    使用executescript 而不是execute

    execute() 只会执行一条 SQL 语句。如果您尝试使用它执行多个语句,它将引发警告。如果您想一次调用执行多个 SQL 语句,请使用 executescript()。

    https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute

    【讨论】:

      【解决方案3】:

      您在查询字符串的中间有一个;, - 这是一个无效的语法。如果要使用命名参数绑定,请将字典作为第二个参数传递给 execute

      sql = "INSERT INTO customer (name, email) VALUES (:name, :email)"
      cursor.execute(sql, {'name':user_name, 'email':user_email})
      

      【讨论】:

      • 它有效。但我不明白为什么这不起作用:sql = "INSERT INTO customer (name, email) VALUES (user_name, user_email)" cursor.execute(sql)
      • 因为您没有为查询插值参数。它应该类似于sql = "INSERT INTO customer (name, email) VALUES ('%s', '%s')" % (user_name, user_email)
      【解决方案4】:

      试试这个:

      sql = """INSERT INTO customer
          (name, email) VALUES (?,?)"""
      
      cursor.execute(sql, (user_name, user_email))
      

      【讨论】:

        【解决方案5】:
        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()
        

        【讨论】:

        • 你能在答案中加入一些描述吗?仅代码的答案被认为是低质量的
        猜你喜欢
        • 2023-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 1970-01-01
        • 2014-01-23
        相关资源
        最近更新 更多