gpt4 book ai didi

python - cursor.rowcount 给出 'int' object is not callable 错误

转载 作者:行者123 更新时间:2023-11-28 19:30:37 25 4
gpt4 key购买 nike

我从 sqlite3 数据库中选择值并打印游标的计数。然后它给出错误“‘int’对象不可调用”

strq="select * from tblsample1"
self.con = sqlite3.connect('mydb.sqlite')
self.cur = self.con.cursor()
self.cur.execute(strq)
print(self.cur.rowcount())

报错

TypeError: 'int' object is not callable

最佳答案

阅读documentation小心! rowcount 是一个属性,因此请将您的代码更正为:

 print(self.cur.rowcount)

Cursor.rowcount Although the Cursor class of the sqlite3 module implements this attribute, the database engine’s own support for the determination of “rows affected”/”rows selected” is quirky.


This includes SELECT statements because we cannot determine the number of rows a query produced until all rows were fetched.


所以你可以修改你的代码来使用fetchall :

self.cur.execute(strq) 
data = self.cur.fetchall()
print len(data)

关于python - cursor.rowcount 给出 'int' object is not callable 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6569313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com