简体   繁体   English

sqlalchemy cx_oracle无法获得结果

[英]sqlalchemy cx_oracle cannot get results

sqlalchemy+cx_Oracle may not be in your domain. sqlalchemy + cx_Oracle可能不在您的域中。 However, if you can help me giving few web links/helps will be nice. 但是,如果您能帮助我,提供很少的Web链接/帮助会很好。

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' 追溯(最近一次调用):TypeError中的文件“”,第1行:'int'对象不可调用错误关闭光标追溯:追溯(最新调用最后一次):AttributeError:'cx_Oracle.Cursor'对象没有属性'lastrowid'

First of all, rowcount is an attribute , so you sould use in this way: 首先,rowcount是一个attribute ,因此您可以通过以下方式使用:

print(result.rowcount)

But it will return 0. Why ? 但是它将返回0。 为什么

Because is only useful in an UPDATE or DELETE statement. 因为仅在UPDATEDELETE语句中有用。 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. 与Python DBAPI所说的相反,它不返回SELECT语句的结果中可用的行数,因为在对行进行无缓冲时,DBAPI不支持此功能。

How can I get the rowcount of a SELECT statement? 如何获得SELECT语句的行数?

You can SELECT with COUNT in this way: 您可以按以下方式用COUNT 选择

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

It will return a ResultProxy. 它将返回一个ResultProxy。 But if you want an int result, you could do: 但是,如果您想要一个int结果,则可以执行以下操作:

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. 如您所知,这是一条SELECT COUNT语句(它只会返回一个字段),您可以将第一个[0]设置为第二个,将RowProxy解析为int。

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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