0

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!

6
  • 1
    Does this answer your question? Python : How to insert a dictionary to a sqlite database? Commented May 27, 2021 at 22:30
  • 1
    Does this answer your question? Python and SQLite: insert into table Commented May 27, 2021 at 22:30
  • 1
    You're not passing your dict as an argument to execute... Gotta pay attention to your parenthesis. Commented May 27, 2021 at 22:32
  • Both answers above are good and I've seen them but still not manage to execute the code. @Shawn What do you mean? Commented May 27, 2021 at 22:34
  • I've pasted the Error message, tried to google it but didn't found anything helpful. Commented May 27, 2021 at 22:35

1 Answer 1

2

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'},)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.