简体   繁体   中英

sqlalchemy cx_oracle cannot get results

sqlalchemy+cx_Oracle may not be in your domain. However, if you can help me giving few web links/helps will be nice.

from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
import cx_Oracle

engine = create_engine('oracle+cx_oracle://user:passwd@FTSDBLAB')
meta = MetaData()
meta.reflect(bind=engine)
tbl_mgr_theater = Table('mgr_table', meta, autoload=True, autoload_with=engine)

connection = engine.connect()
result = connection.execute(tbl_mgr_theater.select())

print(result.rowcount())

gives the following ERROR:

Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable Error closing cursor Traceback (most recent call last): AttributeError: 'cx_Oracle.Cursor' object has no attribute 'lastrowid'

First of all, rowcount is an attribute , so you sould use in this way:

print(result.rowcount)

But it will return 0. Why ?

Because is only useful in an UPDATE or DELETE statement. Contrary to what the Python DBAPI says, it does not return the number of rows available from the results of a SELECT statement as DBAPIs cannot support this functionality when rows are unbuffered.

How can I get the rowcount of a SELECT statement?

You can SELECT with COUNT in this way:

result = connection.execute(tbl_mgr_theater.select().count())

It will return a ResultProxy. But if you want an int result, you could do:

result=[x for x in connection.execute(tbl_mgr_theater.select().count())][0][0]

As you know is a SELECT COUNT statement (it will only return one field), you could set the first [0] , the second is to parse the RowProxy to int.

Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM