MY PROBLEM: I want to create a new column in an existing table with values from a python list. My SQL table looks like this:
gender age cholesterol smoke
1 13 1 0
1 45 2 0
0 1 2 1
And I have values in a python list: new_data = [1,2,3] I want to create a new column in the SQL table and then populate it with the values in the list. So the final table should look like this:
gender age cholesterol smoke new_data
1 13 1 0 1
1 45 2 0 2
0 1 2 1 3
SO FAR: I've tried to create a new column and then try to pass the values:
import sqlite3
con = sqlite3.connect('database.db')
curs = con.cursor()
curs.execute('''ALTER TABLE demo ADD COLUMN new_date''')
con.commit()
new_data = [1,2,3]
curs.execute("INSERT INTO demo(new_date) VALUES(?)", new_data))
But it doesn’t work and gives this error:
ProgrammingError: Incorrect number of bindings supplied.
What should I do?
Here is the dump for the example data:
df = pd.DataFrame(data = {'gender' : [1, 1, 0],
'age' : [13, 45, 1],
'cholesterol' : [1, 2, 2],
'smoke' : [0, 0, 1]
})