为什么我的 python 列表不能从循环中插入到我的 SQLITE 数据库中?
Why wont my python list be inserted into my SQLITE database from a loop?
我有一段非常困惑的时间,我想将 python 列表中的默认数据添加到我的 table。然而,每次添加列表数据都会失败,但我可以使用 for 循环添加硬编码数据,我只是不明白为什么它不适用于列表数据。
这有效,并更新数据库:
categories=["test 1","test 2","test 3","test 4"]
cur = db.cursor()
for category in categories:
cur.execute("INSERT INTO CATEGORIES (name) VALUES ('category')")
db.commit()
cur.close()
这不起作用:
categories=["test 1","test 2","test 3","test 4"]
cur = db.cursor()
for category in categories:
cur.execute("INSERT INTO CATEGORIES (name) VALUES (?)",category)
db.commit()
cur.close()
我的类别 table 有一个设置为自动递增的 ID 和一个名称列。我很困惑。需要一些帮助吗?
您需要将类别包装在一个 1 元组中(并且您需要一个 N 元组用于查询中的 N 个参数)。
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE CATEGORIES (name TEXT)")
categories = ["test 1", "test 2", "test 3", "test 4"]
print("Inserting...")
cur = db.cursor()
for category in categories:
cur.execute("INSERT INTO CATEGORIES (name) VALUES (?)", (category,))
db.commit()
print("Retrieving...")
for row in db.execute("SELECT * FROM CATEGORIES"):
print(row)
打印出来
Inserting...
Retrieving...
('test 1',)
('test 2',)
('test 3',)
('test 4',)
更简洁的写法是使用 executemany
.
生成器 ((cat,) for cat in categories)
在这里执行相同的元组包装。
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE CATEGORIES (name TEXT)")
categories = ["test 1", "test 2", "test 3", "test 4"]
print("Inserting...")
db.executemany(
"INSERT INTO CATEGORIES (name) VALUES (?)",
((cat,) for cat in categories),
)
db.commit()
print("Retrieving...")
for row in db.execute("SELECT * FROM CATEGORIES"):
print(row)
我有一段非常困惑的时间,我想将 python 列表中的默认数据添加到我的 table。然而,每次添加列表数据都会失败,但我可以使用 for 循环添加硬编码数据,我只是不明白为什么它不适用于列表数据。
这有效,并更新数据库:
categories=["test 1","test 2","test 3","test 4"]
cur = db.cursor()
for category in categories:
cur.execute("INSERT INTO CATEGORIES (name) VALUES ('category')")
db.commit()
cur.close()
这不起作用:
categories=["test 1","test 2","test 3","test 4"]
cur = db.cursor()
for category in categories:
cur.execute("INSERT INTO CATEGORIES (name) VALUES (?)",category)
db.commit()
cur.close()
我的类别 table 有一个设置为自动递增的 ID 和一个名称列。我很困惑。需要一些帮助吗?
您需要将类别包装在一个 1 元组中(并且您需要一个 N 元组用于查询中的 N 个参数)。
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE CATEGORIES (name TEXT)")
categories = ["test 1", "test 2", "test 3", "test 4"]
print("Inserting...")
cur = db.cursor()
for category in categories:
cur.execute("INSERT INTO CATEGORIES (name) VALUES (?)", (category,))
db.commit()
print("Retrieving...")
for row in db.execute("SELECT * FROM CATEGORIES"):
print(row)
打印出来
Inserting...
Retrieving...
('test 1',)
('test 2',)
('test 3',)
('test 4',)
更简洁的写法是使用 executemany
.
生成器 ((cat,) for cat in categories)
在这里执行相同的元组包装。
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE CATEGORIES (name TEXT)")
categories = ["test 1", "test 2", "test 3", "test 4"]
print("Inserting...")
db.executemany(
"INSERT INTO CATEGORIES (name) VALUES (?)",
((cat,) for cat in categories),
)
db.commit()
print("Retrieving...")
for row in db.execute("SELECT * FROM CATEGORIES"):
print(row)