【问题标题】:fetchall() not working even with values in database即使使用数据库中的值,fetchall()也不起作用
【发布时间】: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
  • 除了上述注释之外,您还应该始终提及插入的目标列。

标签: python sql sqlite


【解决方案1】:

您需要执行查询,然后使用fetchall()。例如:

info = c.execute("SELECT * FROM persoane;").fetchall()
print(info)

否则,您不会从数据库中选择任何内容,并且您的 c 光标不知道您要获取什么。

【讨论】:

    猜你喜欢
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多