cursor.rowcount赋予'int'对象不可调用的错误

KIR*_*K J 2 python

我从sqlite3数据库中选择了值并打印了cursor的计数.然后它给出了一个错误"'int'对象不可调用"

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

给出错误

TypeError:'int'对象不可调用

Art*_*nka 18

仔细阅读文档!rowcount是一个属性,因此将您的代码更正为:

 print(self.cur.rowcount)
Run Code Online (Sandbox Code Playgroud)

Cursor.rowcount虽然sqlite3模块的Cursor类实现了这个属性,但数据库引擎自己支持确定"受影响的行"/"选择的行"是古怪的.


这包括SELECT语句,因为在获取所有行之前,我们无法确定查询生成的行数.


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

self.cur.execute(strq) 
data = self.cur.fetchall()
print len(data)
Run Code Online (Sandbox Code Playgroud)

  • 根据python文档:'根据Python DB API Spec的要求,如果没有对游标执行executeXX()或者最后一个操作的rowcount不能通过接口"."确定,则rowcount属性为-1. (2认同)

Jac*_*cob 12

print(self.cur.rowcount)
Run Code Online (Sandbox Code Playgroud)

self.cur.rowcount 是一个整数,而不是一个函数.

http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.rowcount

编辑

文档回答您的编辑:

根据Python DB API Spec的要求,如果没有对游标执行executeXX()或者最后一个操作的rowcount不能被接口确定,则rowcount属性"为-1".

这包括SELECT语句,因为在获取所有行之前,我们无法确定查询生成的行数.