2007년 07월 24일
Oralce 에서 MySql 처럼 결과를 사전으로
Python 에서 Mysql 을 사용해 보면
import MySQLdb
db = MySQLdb.connect(user='root',passwd='apmsetup',db='xxx')
cursor =db.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('select * from data')
recs = cursor.fetchall()
for rec in recs:
for type,value in rec.items():
if len(value):
print type,value
cursor.close()
DictCursor 라는 것이 보인다.
이건 select 결과를 사전으로 받아 올수 있다는 것인데 무지 편리하게 사용될수 있다.
근데 Python Oracle 모듈에서는 이 기능을 지원하지 않는다.
하지만 간단히 사용할수가 있다.
for row in curs.fetchall():
row = dict(zip([d[0] for d in curs.description], row))
위의 두줄이 해결의 실마리가 되겠다. 참조 여기
import cx_Oracle
connection = cx_Oracle.connect("xxxx", "xxxxx", "ORCL")
cursor = connection.cursor()
cursor.execute("SELECT * FROM TEST");
recs = cursor.fetchall()
for rec in recs:
row = dict(zip([d[0] for d in cursor.description], rec))
print row
break
cursor.close()
이상 -----
# by | 2007/07/24 15:17 | Python | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]