我想打印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
有人可以帮我解决这个问题吗?相当新的。
答案 0 :(得分:0)
我认为您有编码格式问题(可能的Unicode)
- >如果你尝试这个查询,你有相同的结果吗?:
SELECT cast(TABLE_NAME as varchar(255) ccsid 1147) TABLE_NAME FROM SYSIBM.SYSTABLES WHERE type = 'T' AND name= 'Tables'
- >也许你可以试试这个:
ibm_db.exec_immediate(conn, sql).decode('cp-1251')