我正在编写一个脚本,将获胜/失败结果插入到 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/