【问题标题】:OperationalError: near "?": syntax error sqlite3 errorOperationalError:“?”附近:语法错误 sqlite3 错误
【发布时间】:2026-02-10 04:45:01
【问题描述】:

我试图通过调用文件的函数在表中添加两个值。该文件有以下代码:

import sqlite3
con = sqlite3.Connection('rdb')
cur = con.cursor()
def insert(s):
    cur.execute("create table if not exists customerorder(no number primary 
    key,menuitems varchar(40))")
    c=1
    for i in s:
        print c
        print i
        cur.execute("insert into customerorder(?,?)",(int(c),i))
        c += 1 

def fetch():
    cur.execute("select * from customerorder")
    print cur.fetchall()

这里的's'是一个列表。 有 s[0]='simple book' 和 s[1] = 'advance book'。

【问题讨论】:

    标签: python-2.7 sqlite


    【解决方案1】:
    insert into customerorder(?,?)
    

    您需要将其更改为类似

    insert into customerorder values(?,?)
    

    表名后括号中的第一个列表是列列表,列名不能有? 变量。

    【讨论】: