【发布时间】:2021-06-26 10:01:27
【问题描述】:
我正在使用 sqlite3 在 python 中工作,我正在尝试创建一个数据库。
我创建了一个表:
c.execute("CREATE TABLE persoane (nume text, varsta integer, salariu integer)")
然后我将值添加到表中:
c.execute("INSERT INTO persoane VALUES('David', 16, 1000)")
当我尝试使用 fetchall() 从数据库中获取值时,我得到一个空列表 []
我知道这些值已添加,因为我使用在线程序对其进行了验证。
这是完整的代码供参考:
import sqlite3
conn = sqlite3.connect('baza_date.db')
c = conn.cursor()
c.execute("CREATE TABLE persoane (nume text, varsta integer, salariu integer)")
conn.commit()
c.execute("INSERT INTO persoane VALUES('David', 16, 1000)")
conn.commit()
print(c.fetchall())
【问题讨论】:
-
您需要执行查询来检索数据,例如
SELECT * FROM persoane。 -
除了上述注释之外,您还应该始终提及插入的目标列。