KIRAN K J
KIRAN K J

Reputation: 732

cursor.rowcount gives 'int' object is not callable error

i selected values from sqlite3 database and print the count of cursor.then it gives an error " 'int' object is not callable"

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

gives an error

TypeError: 'int' object is not callable

Upvotes: 5

Views: 56188

Answers (4)

Ariel Huang
Ariel Huang

Reputation: 21

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

Upvotes: 0

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29121

Read documentation carefully! rowcount is an attribute so correct your code to:

 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.


So you can modify your code to use fetchall:

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

Upvotes: 22

Jacob
Jacob

Reputation: 43299

print(self.cur.rowcount)

self.cur.rowcount is an integer, not a function.

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

Edit

The documentation answers your edit:

As required by the Python DB API Spec, the rowcount attribute “is -1 in case no executeXX() has been performed on the cursor or the rowcount of the last operation is not determinable by the interface”.

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

Upvotes: 12

Blair
Blair

Reputation: 15818

The error message says it all really. Don't call the rowcount value, just access it:

print(self.cur.rowcount)

Upvotes: 3

Related Questions