【发布时间】:2021-03-21 16:33:07
【问题描述】:
我的问题:我想在现有表中使用 python 列表中的值创建一个新列。我的 SQL 表如下所示:
gender age cholesterol smoke
1 13 1 0
1 45 2 0
0 1 2 1
我在 python 列表中有值:new_data = [1,2,3] 我想在 SQL 表中创建一个新列,然后用列表中的值填充它。所以决赛桌应该是这样的:
gender age cholesterol smoke new_data
1 13 1 0 1
1 45 2 0 2
0 1 2 1 3
到目前为止:我尝试创建一个新列,然后尝试传递值:
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))
但它不起作用并给出这个错误:
ProgrammingError: Incorrect number of bindings supplied.
我该怎么办?
这里是示例数据的转储:
df = pd.DataFrame(data = {'gender' : [1, 1, 0],
'age' : [13, 45, 1],
'cholesterol' : [1, 2, 2],
'smoke' : [0, 0, 1]
})
【问题讨论】:
-
这是不可能的 - INSERT INTO 用于创建新行而不是向现有行添加数据。
-
那么,我应该使用@norie 什么函数?
-
你的 SQLite 版本是多少?
-
sqlite3.sqlite_version: 3.33.0 @forpas