【问题标题】:Python - Inserting dictionary into SQLite3Python - 将字典插入 SQLite3
【发布时间】: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 database sqlite


【解决方案1】:

你写过:

self.cursor.execute( 'insert ...' ), var_dict

var_dict 没有作为参数传递给execute。它在括号外。相反,您是executevar_dict 的结果的making a two element tuple,然后将其丢弃。

您想像这样将 var_dict 传递给执行。

self.cursor.execute( 'insert ...', var_dict )

下面是对差异的简要说明。

>>> var_dict = dict(foo="bar")
>>> def test(sql, *optional):
...     print(f"Got sql '{sql}' and args {optional}\n")
... 
>>> test('insert...'), var_dict
Got sql 'insert...' and args ()

(None, {'foo': 'bar'})
>>> test('insert...', var_dict)
Got sql 'insert...' and args ({'foo': 'bar'},)

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2017-02-11
    • 2019-01-12
    • 2021-06-05
    • 1970-01-01
    • 2022-10-18
    • 2017-08-16
    • 2021-03-03
    • 2018-06-03
    相关资源
    最近更新 更多