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, 2020 in Python by kartik
• 37,510 points
5,031 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, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How can I get list of values from dict?

Here is an example to get the ...READ MORE

answered Feb 14, 2022 in Python by Nandini
• 5,480 points
858 views
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,720 points
2,041 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
3,240 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 24, 2018 in Python by charlie_brown
• 7,720 points
1,015 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,830 points
602 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
• 58,090 points
735 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, 2020 in Java by Niroj
• 82,880 points
1,089 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, 2020 in Java by Niroj
• 82,880 points
606 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, 2020 in Python by Niroj
• 82,880 points
9,382 views
0 votes
1 answer

How can I get all the request headers in Django?

Hello @kartik,  You can use request.headers to access the HTTP ...READ MORE

answered Jun 30, 2020 in Python by Niroj
• 82,880 points
12,245 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP