[英]Python Insert data from list into SQlite3
I am trying to insert a list with data into a .db file. 我正在尝试将包含数据的列表插入.db文件。
Below is the layout of the db file. 下面是db文件的布局。
Rank is an INTEGER 等级是整数
Description is TEXT 说明为TEXT
I have the following Python code below and SQlite query, 我下面有以下Python代码和SQlite查询,
I am getting the error: 我收到错误消息:
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.
Below is the python code: 以下是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()
I am trying to put the contents of SMH_LADDER[:10] which are strings, and a number from the range(1,11) into db, but cannot get past this error message! 我正在尝试将SMH_LADDER [:10]的内容(它们是字符串)和range(1,11)中的一个数字放入db中,但无法通过此错误消息!
Below is the format of the list SMH_LADDER[:10] 以下是列表SMH_LADDER [:10]的格式
['String1', 'String2', 'String3', 'String4', 'String5', 'String6', 'String7', 'String8', 'String9', 'String10'] ['String1','String2','String3','String4','String5','String6','String7','String8','String9','String10']
Any help would be appreciated! 任何帮助,将不胜感激!
You can't just insert two lists like that. 您不能只插入两个这样的列表。 You need to create a series of INSERT statements that inserts a pair from each list, one pair at a time. 您需要创建一系列INSERT语句,这些语句从每个列表中插入一对,一次插入一对。 You can create the pairs with zip
, then use executemany
to do the inserts. 您可以使用zip
创建对,然后使用executemany
进行插入。
values = zip(range(1,11), SMH_LADDER[:10])
top_ten.executemany('INSERT INTO Top_Ten VALUES(?,?)', values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.