テーブルのすべての列をdb2のデータベースに印刷したいとします。これは私がこれを行うために今持っている現在のコードです。DB2データベースの列を印刷する
#!/usr/bin/python
import ibm_db
from ibm_db import tables, fetch_assoc, exec_immediate
conn = ibm_db.connect("DATABASE=DB;HOSTNAME=9.6.24.89;PORT=50000;PROTOCOL=TCPIP;UID=R1990;PWD=secret;", "", "")
def results(command):
ret = []
result = fetch_assoc(command)
while result:
# This builds a list in memory. Theoretically, if there's a lot of rows,
# we could run out of memory. In practice, I've never had that happen.
# If it's ever a problem, you could use
# yield result
# Then this function would become a generator. You lose the ability to access
# results by index or slice them or whatever, but you retain
# the ability to iterate on them.
ret.append(result)
result = fetch_assoc(command)
return ret # Ditch this line if you choose to use a generator.
t = results(tables(conn))
sql = "SELECT * FROM SYSIBM.SYSTABLES WHERE type = 'T' AND name= 'Tables' " # Using our list of tables t from before...
stmt = ibm_db.exec_immediate(conn, sql)
tuple = ibm_db.fetch_both(stmt)
count = 0
while tuple != False:
print tuple
tuple = ibm_db.fetch_tuple(stmt)
結果はほとんどこのようになります。
\ XFF \ XFF \ XFF \ XFF \ XFF \ XFF \ XFF \ XFF \ XFF \ XFF \ XFF \ x0f \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X01 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ x0f \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X10 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X80 \ XBF \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 ¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥ ¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥ \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 ¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥x00¥ \ x00 \ x00 \ x00
誰かが私を助けてくれますか?かなり新しい。