12

I have this code where I am executing a select sql statement from python code using cx_oracle package:

import cx_Oracle

try:
    cur = conn.cursor()
    result = cur.execute('select * from table1')
    print(str(cur.rowcount))
    print(cur.fetchall())

except Exception as e:
    print(e)

When I execute the above code I see 0 coming in for cur.rowcount but I see following data getting printed for cur.fetchall():

[('185',), ('1860',), ('1908',)]

cx_Oracle package documentation does mention Cursor.rowcount as a valid operation so I am not sure why in my code it is returning 0 even though the data is coming?

3
  • 1
    What if you print(str(cur.rowcount)) after print(cur.fetchall())? Commented Aug 31, 2017 at 16:58
  • @CristiFati Yes then it does print. Why is this behavior? Is it the requirement in cx_Oracle package? In pymysql package it doesn't need to be this way Commented Aug 31, 2017 at 17:18
  • 1
    I've never used cx_Oracle (at least not directly). But, quote from the doc (link you provided): "This read-only attribute specifies the number of rows that have currently been fetched from the cursor ...". Regarding pyMySQL, I know nothing of that either, but they are 2 separate packages wrapping 2 different DB systems.... I don't think there's any restriction for them to behave the same way. Commented Aug 31, 2017 at 17:26

2 Answers 2

9

The documentation states that cursor.rowcount specifies the number of rows that have currently been fetched. Immediately after the cursor.execute() call has been made, no rows have been fetched, so the result is 0. If you call cursor.fetchone() then result will be 1 and if you call cursor.fetchmany(5), then the result will be 6, and so forth (assuming there are enough rows to satisfy your requests, of course!).

Sign up to request clarification or add additional context in comments.

Comments

6

cx-oracle.readthedocs mentioned Cursor.rowcount specified number of rows affected by insert, update and delete statement. You are using a select statement.

cur.execute('select * from table1')
result = cur.fetchall()
print (len(result)) # this will return number of records affected by select statement
print (result)

2 Comments

I don't think thats true. The document says This read-only attribute specifies the number of rows that have currently been fetched from the cursor (for select statements)
Your question is explained here: [link] (bitbucket.org/anthony_tuininga/cx_oracle/issues/16/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.