- 执行SQL语句
方法一:
语法:
调用ibm_db.prepare()函数并传递以下参数:
1.从ibm_db.connect()或ibm_db.pconnect()函数返回的有效数据库连接资源;
2.包含SQL语句的字符串,包括需要变量输入的任何列或谓词值的参数标记(?)。该字符串可以包含由XMLQUERY子句包装的XQuery表达式
3.ibm_db.execute()执行SQL语句
import ibm_db
db_connect = ibm_db.connect("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;
PWD=password;", "", "")
stmt = ibm_db.prepare(db_connect, "UPDATE employee SET bonus = '1000' WHERE job = 'MANAGER'")
# 执行SQL语句
ibm_db.execute(stmt)
方法二:
语法:
调用ibm_db.exec_immediate()函数并传递以下参数:
1.从ibm_db.connect()或ibm_db.pconnect()函数返回的有效数据库连接资源;
2.包含SQL语句的字符串。该字符串可以包含由XMLQUERY子句包装的XQuery表达式
import ibm_db
db_connect = ibm_db.connect("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;
PWD=password;", "", "")
# 单条SQL语句
stmt = ibm_db.exec_immediate(db_connect, "UPDATE employee SET bonus = '1000' WHERE job = 'MANAGER'")
# 多条SQL语句
sql = "SELECT XMLSERIALIZE(XMLQUERY('for $i in $t/address where $i/city = \"Olathe\" return <zip>{$i/zip/text()}</zip>' passing c.xmlcol as \"t\") AS CLOB(32k)) FROM xml_test c WHERE id = 1"
stmt = ibm_db.exec_immediate(db_connect, sql)
result = ibm_db.fetch_both(stmt)
- SQL语句结果返回
语句 | 描述 |
---|---|
ibm_db.fetch_tuple() | 返回按列位置索引的元组,表示结果集中的行。列是0索引的 |
ibm_db.fetch_assoc() | 返回按列名索引的字典,表示结果集中的行 |
ibm_db.fetch_both() | 返回一个字典,由列名和位置索引,表示结果集中的行 |
ibm_db.fetch_row() | 将结果集指针设置为下一行或请求的行。使用此函数迭代结果集 |
代码示例:
import ibm_db
db_connect = ibm_db.connect("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;
PWD=password;", "", "")
sql = "SELECT * FROM EMPLOYEE"
stmt = ibm_db.exec_immediate(db_connect, sql)
# 通过调用ibm_db.fetch_tuple()函数从结果集中获取行。
tuple = ibm_db.fetch_tuple(stmt)
while tuple != False:
print "The ID is : ", tuple[0]
print "The name is : ", tuple[1]
# 通过调用ibm_db.fetch_assoc()函数从结果集中根据字典的键获取结果
dictionary = ibm_db.fetch_assoc(stmt)
while dictionary != False:
print "The ID is : ", dictionary["EMPNO"]
# 通过调用ibm_db.fetch_both()函数从结果集中获取行的结果或根据字典的键获取结果
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False:
print "The ID is : ", dictionary["EMPNO"]
print "The Name is : ", dictionary[1]
# 通过调用ibm_db.fetch_row()函数从结果集中进行迭代获取结果
while ibm_db.fetch_row(stmt) != False:
print("The Employee number is : ", ibm_db.result(stmt, 0))
print("The Name is : ", ibm_db.result(stmt, "NAME"))