2010-11-10 12 views
16

जब मैं कर sometingअजगर - mysqlDB, शब्दकोश के रूप में SQLite परिणाम

sqlite.cursor.execute("SELECT * FROM foo") 
result = sqlite.cursor.fetchone() 

की तरह मुझे लगता है कि आदेश कॉलम उन्हें बाहर लाने के लिए सक्षम होने के लिए दिखाई देते हैं याद करने के लिए है, जैसे

result[0] is id 
result[1] is first_name 

है एक शब्दकोश वापस करने के लिए एक रास्ता है? तो मैं इसके बजाय परिणाम ['id'] या इसी तरह का उपयोग कर सकता हूं?

क्रमांकित कॉलम के साथ समस्या यह है कि, यदि आप अपना कोड लिखते हैं तो कोड को बदलने के लिए आपको एक कॉलम डालना पड़ सकता है जैसे परिणाम [1] first_name के लिए अब date_joined हो सकता है, इसलिए सभी कोड अपडेट करना होगा ..

+0

संभावित डुप्लिकेट http://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite -क्वायरी) क्या यह स्क्लाइट या माइस्क्ल के बारे में है? –

उत्तर

5

डेविड बीज़ले का Python Essential Reference में इसका एक अच्छा उदाहरण है।
मैं हाथ में किताब नहीं है, लेकिन मुझे लगता है कि उसके उदाहरण कुछ इस तरह है:

def dict_gen(curs): 
    ''' From Python Essential Reference by David Beazley 
    ''' 
    import itertools 
    field_names = [d[0].lower() for d in curs.description] 
    while True: 
     rows = curs.fetchmany() 
     if not rows: return 
     for row in rows: 
      yield dict(itertools.izip(field_names, row)) 

नमूना उपयोग:

>>> import sqlite3 
>>> conn = sqlite3.connect(':memory:') 
>>> c = conn.cursor() 
>>> c.execute('create table test (col1,col2)') 
<sqlite3.Cursor object at 0x011A96A0> 
>>> c.execute("insert into test values (1,'foo')") 
<sqlite3.Cursor object at 0x011A96A0> 
>>> c.execute("insert into test values (2,'bar')") 
<sqlite3.Cursor object at 0x011A96A0> 
# `dict_gen` function code here 
>>> [r for r in dict_gen(c.execute('select * from test'))] 
[{'col2': u'foo', 'col1': 1}, {'col2': u'bar', 'col1': 2}] 
4

तुम बहुत आसानी से कर सकते हैं। SQLite के लिए: my_connection.row_factory = sqlite3.Row

यह अजगर डॉक्स पर की जाँच करें: http://docs.python.org/library/sqlite3.html#accessing-columns-by-name-instead-of-by-index

अद्यतन:

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sqlite3 
>>> conn = sqlite3.connect(':memory:') 
>>> conn.row_factory = sqlite3.Row 
>>> c = conn.cursor() 
>>> c.execute('create table test (col1,col2)') 
<sqlite3.Cursor object at 0x1004bb298> 
>>> c.execute("insert into test values (1,'foo')") 
<sqlite3.Cursor object at 0x1004bb298> 
>>> c.execute("insert into test values (2,'bar')") 
<sqlite3.Cursor object at 0x1004bb298> 
>>> for i in c.execute('select * from test'): print i['col1'], i['col2'] 
... 
1 foo 
2 bar 
+0

इंटरैक्टिव नमूने के लिए @Adam के लिए धन्यवाद। –

11

mysqlDB में ऐसा करने से आप बस अनुसरण कनेक्ट समारोह कॉल

cursorclass = MySQLdb.cursors.DictCursor 
में जोड़ें
1

एक sqlite3.रो उदाहरण को dict में परिवर्तित किया जा सकता है - परिणामस्वरूप डंप करने के लिए बहुत आसान बेटा

>>> csr = conn.cursor() 
>>> csr.row_factory = sqlite3.Row 
>>> csr.execute('select col1, col2 from test') 
>>> json.dumps(dict(result=[dict(r) for r in csr.fetchall()])) 
+1

यह मुझे एक 'शब्दकोश अद्यतन अनुक्रम तत्व # 0 देता है लंबाई 15 है; 2 आवश्यक है – Tobi

29
import MySQLdb 
dbConn = MySQLdb.connect(host='xyz', user='xyz', passwd='xyz', db='xyz') 
dictCursor = dbConn.cursor(MySQLdb.cursors.DictCursor) 
dictCursor.execute("SELECT a,b,c FROM table_xyz") 
resultSet = dictCursor.fetchall() 
for row in resultSet: 
    print row['a'] 
dictCursor.close 
dbConn.close() 
की [मैं कैसे SQLite क्वेरी से dict मिल सकता है?] (
+0

धन्यवाद, यह काम करता है। – swietyy

+0

'DictCursor' द्वारा' TypeError: unhashable type: 'dict''' में चलाने के बिना 'सेट()' में पुनर्प्राप्त पंक्तियों को जोड़ने का कोई तरीका है? – ray

+1

यह सही और स्वीकार्य उत्तर होना चाहिए। –

संबंधित मुद्दे