I've researched similar links, but still can't figure this out. Most of the other links are trying to insert into multiple rows or columns, but I need this list of tuples in one column.
word_in_python = cat
listOfTuples = [('word1', 12), ('word2', 14), ('word3', 4), ('word5', 23)]
Here is my code:
conn = sqlite3.connect('mydb.db')
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO saved_words (word, data) VALUES (?, ?)", (word_in_python, [listOfTuples]))
conn.commit()
conn.close()
I need the INSERT OR IGNORE because the word column is a unique column so duplicate words should not be entered into the db.
I've tried changing to:
c.execute("INSERT OR IGNORE INTO saved_words (word, data) VALUES (?, ?)", (word_in_python, (listOfTuples, )))
c.executemany("INSERT OR IGNORE INTO saved_words (word, data) VALUES (?, ?)", (word_in_python, [listOfTuples]))
for each in listOfTuples:
c.executemany("INSERT OR IGNORE INTO saved_words (word, data) VALUES (?, ?)", (word_in_python, (each[0], each[1]) ))
None of these work.
The error I am typically getting is:
Incorrect number of bindings supplied. The current statement uses 2, and there are 5 supplied.
I'm also confused by this error. I understand that (?, ?) in my execute line means I am trying to input 2 bindings. My list of tuples has 10 tuples in it, so I am not sure where the 5 supplied bindings are coming from.
Also, my saved_words table had the data column as type BLOB. I thought this would be best for inserting a list.
EDIT
What I want is word_in_python (cat) in one column and in the next column listOfTuples, this is all in one row.