简体   繁体   English

将具有来自 python 列表的值的新列插入到现有 sql 表中

[英]Insert new column with values from a python list into an existing sql table

MY PROBLEM: I want to create a new column in an existing table with values from a python list.我的问题:我想在现有表中使用 python 列表中的值创建一个新列。 My SQL table looks like this:我的 SQL 表如下所示:

   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.我在 python 列表中有值: new_data = [1,2,3]我想在 SQL 表中创建一个新列,然后用列表中的值填充它。 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]
                          })

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.而不是列表new_data = [1,2,3]传递列表列表new_data = [[1, 1], [2, 2], [3, 3]]每个列表的第二项是行号将被更新的行。
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]] .因此,如果您想使用值 100、150、250 更新列,那么new_data应该是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: UPDATE语句将有一个WHERE子句,它将检查每次应该更新哪一行:

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:由于您的 SQLite 版本是 3.33.0,您还可以在UPDATE语句中使用UPDATE...FROM语法:

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 = ?
""" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM