简体   繁体   中英

Python - Inserting dictionary into SQLite3

I got a dictionary with 14 keys.

First Ive created createTableOfRecordsenter function:

    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()

and the table with columns created successfully. After that, I tried to insert the data using the insertRecords function:

    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()

but I didn't get any value inserted into the database table. I got this error message:

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.

Does anyone know what I've done wrong? Thanks!

You've written:

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

var_dict is not being passed as an argument to execute . It is outside the parenthesis. Instead you are making a two element tuple of the result of execute and var_dict and then throwing it out.

You want to pass var_dict into execute like so.

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

Here's a quick illustration of the difference.

>>> 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'},)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM