这个问题说明了一切。以 Python API 为例:
import ibm_db
#setup stuff
conn = ibm_connect(DATABASE, user, password)
stmt = ibm_db.exec_immediate(conn, 'DELETE FROM sometable')
print(ibm_db.num_rows(stmt)) # prints -1
为什么它不打印实际删除的行数?
这个问题说明了一切。以 Python API 为例:
import ibm_db
#setup stuff
conn = ibm_connect(DATABASE, user, password)
stmt = ibm_db.exec_immediate(conn, 'DELETE FROM sometable')
print(ibm_db.num_rows(stmt)) # prints -1
为什么它不打印实际删除的行数?
实际上,这不是一个答案,只是表明该函数确实有效并且 SQLCODE 会导致 python 异常。num_rows()
对您不起作用的事实可能表明您连接到的数据库可能不支持该功能。您可能想要详细描述您的环境:DB2 服务器版本和平台,是本地还是远程数据库,DB2 客户端版本(如果与服务器不同)等。
>>> import ibm_db
>>> conn = ibm_db.connect('TEST',user,password)
>>> stmt = ibm_db.exec_immediate(conn, 'create table t (f int)')
>>> print ibm_db.num_rows(stmt) # DDL statement - num_rows not applicable
-1
>>> stmt = ibm_db.exec_immediate(conn,'delete from t')
>>> print ibm_db.num_rows(stmt) # 0 rows deleted
0
>>> stmt = ibm_db.exec_immediate(conn,'delete from x') # nonexistent table - exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: [IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "USER.X" is an undefined name. SQLSTATE=42704 SQLCODE=-204
>>> stmt = ibm_db.exec_immediate(conn,'insert into t(f) values (1),(2),(3)')
>>> print ibm_db.num_rows(stmt) # 3 rows inserted
3
>>> stmt = ibm_db.exec_immediate(conn,'delete from t')
>>> print ibm_db.num_rows(stmt) # 3 rows deleted
3
>>> ibm_db.close(conn)
True
>>>
DB2 中的负 SQL 代码意味着错误: SQL 代码