python cx_oracle cursor.rowcount返回0,cursor.fetchall返回d

2024-05-28 19:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这段代码,在这里我使用cx_oracle包从python代码执行select sql语句:

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)

当我执行上述代码时,我看到0出现在cur.rowcount中,但我看到以下数据被打印为cur.fetchall()

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

cx_Oracle package documentation确实提到了Cursor.rowcount作为一个有效的操作,所以我不确定为什么在我的代码中,它会返回0,即使数据即将到来?


Tags: 数据代码importsql语句selectoracleprint
2条回答

文档说明cursor.rowcount指定当前已提取的行数。在执行cursor.execute()调用之后,没有获取任何行,因此结果为0。如果调用cursor.fetchone(),则result为1;如果调用cursor.fetchmany(5),则result为6,以此类推(当然,假设有足够的行满足您的请求!)。

cx-oracle.readthedocs提到Cursor.rowcount指定了受insert、update和delete语句影响的行数。您正在使用select语句。

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

相关问题 更多 >

    热门问题