2009-06-05 15 views
6

मैं अजगर लॉगिंग मॉड्यूल के लिए एक हैंडलर पर काम कर रहा हूं। यह अनिवार्य रूप से एक ओरेकल डेटाबेस के लिए लॉग इन करता है।तालिका खाली होने पर cx_oracle में कॉलम जानकारी प्राप्त करना?

मैं cx_oracle का उपयोग कर रहा हूं, और कुछ मुझे नहीं पता कि टेबल खाली होने पर कॉलम मान कैसे प्राप्त करें।

cursor.execute('select * from FOO') 
for row in cursor: 
    # this is never executed because cursor has no rows 
    print '%s\n' % row.description 

# This prints none 
row = cursor.fetchone() 
print str(row) 

row = cursor.fetchvars 
# prints useful info 
for each in row: 
    print each 

उत्पादन होता है:

None 
<cx_Oracle.DATETIME with value [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None 
, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None 
, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]> 
<cx_Oracle.STRING with value [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]> 

अब वर डेटा पर मैं कर सकते हैं की तलाश में डेटा प्रकार देखते हैं, और उनके आकार (नाउंस गिनती?), लेकिन स्तंभ नाम याद आ रही thats।

मैं इस बारे में कैसे जा सकता हूं?

उत्तर

13

मुझे लगता है कि description विशेषता आप जो खोज रहे हैं वह हो सकती है। यह उन tuples की एक सूची देता है जो लौटाए गए डेटा के कॉलम का वर्णन करते हैं। यदि कोई पंक्तियां वापस नहीं आती हैं, तो यह बहुत खुशी से काम करता है, उदाहरण के लिए:

 
>>> import cx_Oracle 
>>> c = cx_Oracle.connect("username", "password") 
>>> cr = c.cursor() 
>>> cr.execute("select * from dual where 1=0") 
<__builtin__.OracleCursor on <cx_Oracle.Connection to user [email protected]>> 
>>> cr.description 
[('DUMMY', <type 'cx_Oracle.STRING'>, 1, 1, 0, 0, 1)] 
संबंधित मुद्दे