/ / मैं उस जानकारी को पुनः प्राप्त करना चाहता हूं जो एक शब्दकोश के रूप में और एक के बिना उस सूचकांक से मेल खाती है: sqlite3 - mysql, database, python-3.x, sqlite3

मैं उस इंडेक्स से संबंधित जानकारी को पुनर्प्राप्त करना चाहता हूं जो एक शब्दकोश के रूप में और बिना किसी के है: sqlite3 - mysql, डेटाबेस, पायथन-3.x, sqlite3

मुझे यह समझ में नहीं आया कि मूल्यों को शब्दकोश के रूप में कैसे लौटाया जा सकता है, उदाहरण के लिए, यदि dic = true यह वापस आ जाएगा {'Idx': 3, 'name': 'me', 'passwd' 'a9993e364706816aba3e25717850c26cd9d0d89d'}

def retrieve(self, idx, dic = True):
"""
returns a tuple with the three
values of the record with index idx, or returns None
if an exception occurred, if the
option dict is False. By default,
dict=True and on return is a dictionary with the
keys ’idx’, ’name’ and ’passwd’.
The values in the dictionary correspond to the fields
in the record with index idx
"""


if dic == False:

query = "SELECT * FROM players WHERE idx = ?"

self.cr.execute(query)

if dic == True:

query = "SELECT * FROM players WHERE idx = ?"

self.cr.execute(query)

self.db.commit()

print(self.cr.fetchall())

मैं एक के रूप में आईडी के साथ समारोह चलाने की कोशिश कीस्ट्रिंग इनपुट और यह मुझे शब्दकोश के बिना टपल लाने में सक्षम था, लेकिन मेरा मानना ​​है कि यह एक प्रारूपण मुद्दा है। किसी भी सहायता की सराहना की जाएगी।

उत्तर:

जवाब के लिए 2 № 1

यदि आप tuple से डिक्शनरी बनाना चाहते हैं, तो आप कुछ कॉलम में अपने कॉलम के नाम स्टोर कर सकते हैं और फिर कॉलम के नाम और आपके रिकॉर्ड tuples से डिक्शनरी बना सकते हैं। zip पसंद

def retrieve(self, idx, dic=True):
query = "SELECT * FROM players WHERE idx = {idx}".format(idx=idx)
self.cr.execute(query)
self.db.commit()

records_tuples = self.cr.fetchall()
if dic:
columns_names = ["idx", "name", "passwd"]
records_dicts = [dict(zip(columns_names, record_tuple))
for record_tuple in records_tuples]
return records_dicts
else:
return records_tuples