2016-09-30 3 views
0

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 \ 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

나는 당신이 (유니 코드 가능) encodage 형식의 문제가 있다고 생각

-> 그리고 당신은이 쿼리를 시도하는 경우에, 당신에게 동일한 결과?

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')