2016-05-23 60 views
0

我试图将带有数据的列表插入到.db文件中。
下面是db文件的布局。Python将列表中的数据插入到SQlite3

排名是INTEGER
说明是TEXT

db layout

下面我有以下Python代码和SQLite查询,
我得到的错误:

  Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__ 
    return self.func(*args) 
    line 136, in DB_Entry 
    top_ten.execute('INSERT INTO Top_Ten VALUES(?,?)', [range(1,11),SMH_LADDER[:10]],) 
InterfaceError: Error binding parameter 0 - probably unsupported type. 

下面是python代码:

def DB_Entry(): 
# Create a connection to the database. 
connection = connect(database = "top_ten.db") 

# Get a cursor on the database. This allows you to execute SQL 
top_ten = connection.cursor() 

top_ten.execute('INSERT INTO Top_Ten VALUES(?,?)', [range(1,11),SMH_LADDER[:10]],) 

# Commit the changes to the database 
connection.commit() 

# Close the cursor. 
top_ten.close() 

# Close the database connection. 
connection.close() 

我试图把SMH_LADDER的内容[10]从范围(1,11),这是字符串,一个数字分贝,但不能让过去此错误消息!

下面是列表SMH_LADDER的格式[:10]
[ '字符串1', '字符串2', 'STRING3', '串,4', 'STRING5', 'String6', 'String7', 'String8' ,'String9','String10']

任何帮助将不胜感激!

回答

2

你不能只插入两个这样的列表。您需要创建一系列INSERT语句,从每个列表中插入一对,每次一对。您可以使用zip创建对,然后使用executemany进行插入。

values = zip(range(1,11), SMH_LADDER[:10]) 
top_ten.executemany('INSERT INTO Top_Ten VALUES(?,?)', values) 
+0

感谢Daniel的回复,请参阅pastebin链接,我现在正在收到不同的错误消息。 http://pastebin.com/7m1YTNX4 – deluxenathan

+0

注意:我正在使用Tkinter – deluxenathan

+0

您没有将其更改为'executemany'。 –

相关问题