python - 使用 Python 从 sqlite3 DB 中提取值

标签 python sqlite

我正在编写一个脚本,将获胜/失败结果插入到 sqlite3 数据库中。我还尝试从所述数据库表中提取赢/输计数。但是,我收到一个错误,无法从 sqlite3 游标对象转换为字符串以连接到我的 win 消息中。我肯定在这里做错了什么。有什么帮助吗?

        cur.execute("INSERT INTO WINS(winner) VALUES('Computer')")
        numberWins = cur.execute("select count(*) from WINS where winner = 'Computer'")
        print("******************************\n" + numberWins + "Computer Wins\n******************************")

我收到此错误:TypeError:无法连接“str”和“sqlite3.Cursor”对象

最佳答案

您需要调用.fetchone().fetchall()关于查询。然后返回一个元组或元组列表。因此,您可以使用 numberWins[0]numberWins[0][0] 访问该值。

numberWins = cur.execute("select count(*) from WINS where winner = 'Computer'").fetchone()
print("************\n" + str(numberWins[0]) + "Computer Wins\n************")

或者您可以使用字符串格式:

print("************\n {} Computer Wins\n************".format(numberWins[0]))

关于python - 使用 Python 从 sqlite3 DB 中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35513728/

相关文章:

Python xmlrpclib 在传输时将 datetime 对象更改为 DateTime 实例

python - 如何在 Jupyter Notebook 中加载 CSV 文件?

python - GF(2 ^ n)上的有限域算法?

sqlite - 不将 SQLite DB 放在 NFS 上的 createrepo 解决方法?

python - 如何将热图注释为子图?

python - 可选地守护 Python 进程

c# - 删除sqlite中的where in子句

sqlite - 您能否在查询数据库后将原本是 bool 的 sqlite 数据库中的 int 解析回 bool ?

sql - SQLite 中的 CEIL 和 FLOOR

ios - 哪里不能使用核心数据?是否可以像MySql那样把核心数据当做数据库使用?