#!/usr/bin/env python
# -*- coding:utf-8 -*-#########################
# excute python2.7.13
# sudo pip install ibm_db
#########################
import ibm_dbdef getDateFromDb2(uid,pwd):
pass
try:
conn = ibm_db.connect("DATABASE=seat;HOSTNAME=10.221.196.xx;PORT=50000;PROTOCOL=TCPIP;\
UID=xxx;PWD=xxx", "", "")
except:
print("connect failed,%s" % ibm_db.conn_errormsg())
else:
sql = "SELECT * FROM TM_SEAT_CONDITION WHERE airline = 'CA' and "\
"FLT_REG = 'B6822' and MARK_FOR_DELETE='N'"
try:
stmt = ibm_db.exec_immediate(conn, sql)
dictionary = ibm_db.fetch_both(stmt)
while dictionary!=False:
for i in range(0, 15):
if i in (10, 12):
print(dictionary.get(i).strftime('%Y-%m-%d %H:%M:%S')),
else:
print(dictionary.get(i)),
print("\n")
dictionary = ibm_db.fetch_both(stmt)
except:
print("stmt failed,%s" % ibm_db.stmt_errormsg())
finally:
if ibm_db.active(conn):
ibm_db.close(conn)
print("database close success!")
# -------------------提取查询结果有四种方式-----------------------#
# 1.先调用fetch_row(stmt),有结果返回true否则返回false,再调用ibm_db.result(stmt, col_idx)提取结果
# ibm_db.fetch_row(stmt)
# result = ibm_db.result(stmt, 0)
# 2.调用fetch_both(stmt)提取一行,返回的结果是一个字典,取值可以用列名或索引 result["MOBILE"]或 result[0]
# result = ibm_db.fetch_both(stmt)
# 3.调用fetch_assoc(stmt)提取一行,返回的是一个字典,取值只能用列明 result["MOBILE"]
# result = ibm_db.fetch_assoc(stmt)
# 4.调用fetch_tuple(stmt)提取一行,返回的是一个元组,取值只能用索引 result[0]
# result = ibm_db.fetch_tuple(stmt)
# 注: ibm_db没办法一次提取多行,提取多行数据,需要循环提取