33
votes

I am trying to get the rowcount of a sqlite3 cursor in my Python3k program, but I am puzzled, as the rowcount is always -1, despite what Python3 docs say (actually it is contradictory, it should be None). Even after fetching all the rows, rowcount stays at -1. Is it a sqlite3 bug? I have already checked if there are rows in the table.

I can get around this checking if a fetchone() returns something different than None, but I thought this issue would be nice to discuss.

Thanks.

6
"actually it is contradictory..." Please provide a reference or a link for this. - S.Lott
docs.python.org/3.0/library/… Maybe I got it wrong but the last paragraph of Cursor.rowcount says "This includes SELECT statements because we cannot determine the number of rows a query produced until all rows were fetched." So I guessed that after fetching them witch fetchone() or fetchall() I would get an updated rowcount. thanks - Hiperi0n
When using the package mysql.connector the rowcount attribute is -1 after an .execute call of a SELECT-statement, but after the fetchall() call the actual number of rows in the fetched data is returned. Just an observation... - Duffau

6 Answers

24
votes

From the documentation:

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.

That means all SELECT statements won't have a rowcount. The behaviour you're observing is documented.

EDIT: Documentation doesn't say anywhere that rowcount will be updated after you do a fetchall() so it is just wrong to assume that.

21
votes
cursor = newdb.execute('select * from mydb;')
print len(cursor.fetchall())

The fetchall() will return a list of the rows returned from the select. Len of that list will give you the rowcount.

8
votes

Instead of "checking if a fetchone() returns something different than None", I suggest:

cursor.execute('SELECT * FROM foobar')
for row in cursor:
   ...

this is sqlite-only (not supported in other DB API implementations) but very handy for sqlite-specific Python code (and fully documented, see http://docs.python.org/library/sqlite3.html).

8
votes

May better count the rows this way:

 print cur.execute("SELECT COUNT(*) FROM table_name").fetchone()[0]
1
votes

I've spent too long trying to find this, if you use this line you would want to use the .rowcount it should work for you. I'm using it to check if my statement will return any data.

    if (len(cursor.execute(sql).fetchall())) < 1: # checks there will be data by seeing if the length of the list make when getting the data is at least 1
        print("No data gathered from statement") #
    else:
       #RUN CODE HERE
-5
votes

Using PYCharm, in debug mode, if you put your cursor on your cursor variable, a small + will appear, when you click on it, you see different properties of your object.

In my cursor of 1 element, I see:

rowcount={int}
arraysize={int}1

So, in your code, just use:

print(cursor.arraysize)
1