0
votes

I always got this error when I trying to run my python script:

c.execute("INSERT INTO test (word) VALUES (?)", (word)) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied.

Any idea what is the problem?

import itertools, time, sqlite3

def main():    
    try:
        print ("Still running, please wait...")
        start_time = time.time()

        con = sqlite3.connect("dictionary.db")
        c = con.cursor()
        c.execute("CREATE TABLE test (string char(10))")

        for word in itertools.imap(''.join, itertools.product('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@&#!*', repeat=4)):
            c.execute("INSERT INTO test (word) VALUES (?)", (word))
            con.commit()

    finally:
        print ("Done! Check the txt file!\n")
        end_time = time.time()
        print "Processing time was: ", end_time - start_time

if __name__ == "__main__": main()
1

1 Answers

3
votes

Change

c.execute("INSERT INTO test (word) VALUES (?)", (word))

Into

c.execute("INSERT INTO test (word) VALUES (?)", (word,))

Explanation:

(word) is evaluated to simply word which is a string in your case. (word,) is evaluated to a single-element tuple, which is what SQLite expects.

(Sqlite expects a tuple with as many elements as there are question marks in the statement.)