How can I get dict from sqlite query?

0 votes
db = sqlite.connect("test.sqlite")
res = db.execute("select * from table")

With iteration I get lists coresponding to the rows.

for row in res:
    print row

I can get name of the columns

col_name_list = [tuple[0] for tuple in res.description]

But is there some function or setting to get dictionaries instead of list?

{'col1': 'value', 'col2': 'value'}

or I have to do myself?

Apr 30 in Python by kartik
• 11,890 points
146 views

1 answer to this question.

0 votes

Hii,

Even using the sqlite3.Row class-- you still can't use string formatting in the form of:

print "%(id)i - %(name)s: %(value)s" % row

In order to get past this, I use a helper function that takes the row and converts to a dictionary. I only use this when the dictionary object is preferable to the Row object (e.g. for things like string formatting where the Row object doesn't natively support the dictionary API as well). But use the Row object all other times.

def dict_from_row(row):
    return dict(zip(row.keys(), row))      
answered Apr 30 by Niroj
• 23,950 points

Related Questions In Python

0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,760 points
242 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
1,633 views
0 votes
1 answer

How can I get a list of locally installed Python modules?

Solution My 50 cents for getting a pip freeze-like ...READ MORE

answered May 23, 2018 in Python by charlie_brown
• 7,760 points
284 views
0 votes
1 answer

How can I Get Laplacian pyramid using opencv

As far as I can see you ...READ MORE

answered Sep 4, 2018 in Python by Priyaj
• 57,530 points
534 views
0 votes
1 answer

What are the prerequisites to learn Hadoop in java perspective?

In my day job, I've just spent ...READ MORE

answered Oct 11, 2018 in Big Data Hadoop by Frankie
• 9,810 points
87 views
0 votes
1 answer

How is inheritance in C++ different than that in Java?

The purpose of inheritance is same for ...READ MORE

answered Feb 6, 2019 in Java by Priyaj
• 57,530 points
87 views
0 votes
1 answer

What is the reason for no ConcurrentHashSet against ConcurrentHashMap?

Hello, There's no built in type for ConcurrentHashSet because you ...READ MORE

answered Apr 8 in Java by Niroj
• 23,950 points
28 views
0 votes
1 answer

How to pass an object from one activity to another on Android?

Hello @kartik, Implement your class with Serializable. Let's ...READ MORE

answered Apr 8 in Java by Niroj
• 23,950 points
36 views
0 votes
1 answer

How can I get the domain name of my site within a Django template?

Hello kartik, The variation of the context processor ...READ MORE

answered Apr 23 in Python by Niroj
• 23,950 points
151 views
0 votes
1 answer

How do I get user IP address in django?

Hii Kartik, check uwsgi + django source code and pass static param ...READ MORE

answered Apr 23 in Python by Niroj
• 23,950 points
241 views