【发布时间】:2021-05-28 08:00:20
【问题描述】:
我有一本有 14 个键的字典。
我首先创建了createTableOfRecordsenter 函数:
def createTableOfRecords(self):
create_table = '''CREATE TABLE IF NOT EXISTS records(Budget TEXT , Commitment TEXT, Contract_Type TEXT , Customer_Type TEXT, Duration TEXT , Goals TEXT, Pace TEXT ,
Procedures_and_Regulations TEXT, Resources TEXT , Scope TEXT, Team_Availability TEXT , Team_Distribution TEXT, Team_Size TEXT , Uncertainty TEXT);'''
self.cursor.execute(create_table)
self.connection.commit()
以及成功创建列的表。
之后,我尝试使用insertRecords 函数插入数据:
global var_dict
var_dict = dict(Budget="Fixed",
Commitment="Low",
Contract_Type="Hybrid",
Customer_Type="Market",
Duration="Long",
Goals="Unclear",
Pace="Fast",
Procedures_and_Regulations="None",
Resources="Standart",
Scope="Rigid",
Team_Availability="Fully",
Team_Distribution="Global",
Team_Size="Small",
Uncertainty="Predictable")
def insertRecords(self):
self.cursor.execute('INSERT INTO records (Budget,Commitment,Contract_Type,Customer_Type,Duration,Goals,Pace,'
'Procedures_and_Regulations,Resources,Scope,Team_Availability,'
'Team_Distribution,Team_Size,Uncertainty) '
'VALUES (:Budget, :Commitment, :Contract_Type, :Customer_Type, :Duration, '
':Goals, :Pace, :Procedures_and_Regulations, :Resources, :Scope, :Team_Availability, '
':Team_Distribution, :Team_Size, :Uncertainty);'), var_dict
self.connection.commit()
但我没有将任何值插入到数据库表中。 我收到此错误消息:
self.cursor.execute('INSERT INTO records (Budget,Commitment,Contract_Type,Customer_Type,Duration,Goals,Pace,' sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 14, and there are 0 supplied.
有人知道我做错了什么吗? 谢谢!
【问题讨论】:
-
这能回答你的问题吗? Python and SQLite: insert into table
-
你没有将你的 dict 作为参数传递给
execute... 必须注意你的括号。 -
以上两个答案都很好,我已经看到了,但仍然无法执行代码。 @Shawn 你什么意思?
-
我已经粘贴了错误消息,尝试谷歌搜索但没有发现任何有用的信息。