2016-11-17 37 views
0

我试图从整数列表创建SQLite中一列,但出现以下错误:如何从列表添加到数据库?

"sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied." 

这里是我的代码:

conn = sq.connect('test.db') 
c = conn.cursor() 

def create_table(): 
c.execute('CREATE TABLE IF NOT EXISTS table(listItem VALUE)') 


def data_entry(): 
x = 0 
list1 = [61,33,4,5] 
n = [l for l in list1] 
while x < len(list1): 
    listItem = n 
    c.execute("INSERT INTO tell(listItem) VALUES (?)", 
      (listItem)) 
    x += 1 
conn.commit() 


create_table() 
data_entry() 
c.close() 
conn.close() 

提前感谢! :)

回答

2

如果我理解正确的话,要循环的列表,而不是插入整个列表一行

for l in list1: 
    c.execute("INSERT INTO tell(listItem) VALUES (?)", 
     (l,)) 
c.close() 
conn.commit() 

而且你的CREATE TABLE语句应创建tell,因为table是保留字,这就是你插入到

+0

这样做的伎俩 - 非常感谢你! :) –

相关问题