1

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]
                          })
4
  • That's not possible - INSERT INTO is for creating new rows not adding data to existing rows. Commented Mar 21, 2021 at 17:36
  • So, what function should I use @norie? Commented Mar 21, 2021 at 17:38
  • What is your version of SQLite? Commented Mar 21, 2021 at 20:25
  • sqlite3.sqlite_version: 3.33.0 @forpas Commented Mar 21, 2021 at 20:54

1 Answer 1

1

You must update the table after you add the new column and not insert new rows.

Instead of the list new_data = [1,2,3] pass a list of lists new_data = [[1, 1], [2, 2], [3, 3]] with the 2nd item of each list is the row number of the row that will be updated.
So, if you wanted to update the column with the values 100, 150, 250 then new_data should be new_data = [[100, 1], [150, 2], [250, 3]].

The UPDATE statement will have a WHERE clause that will check which row should be updated each time:

new_data = [[1, 1], [2, 2], [3, 3]]
sql = """
UPDATE demo AS d
SET new_data = ?
WHERE (SELECT COUNT(*) FROM demo dd WHERE dd.rowid <= d.rowid) = ?
"""
curs.executemany(sql, new_data)
con.commit() 

Since your version of SQLite is 3.33.0 you could also use the UPDATE...FROM syntax in your UPDATE statement:

sql = """
UPDATE demo AS d1
SET new_data = ?
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY rowid) rn FROM demo) AS d2
WHERE d2.rowid = d1.rowid AND rn = ?
""" 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Btw I've updated my SQLite version, syntax is more friendly!

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.