[Tutor] Adding items from a cursor to a dict?

Timo timomlists at gmail.com
Mon Nov 12 12:17:52 CET 2012


Op 12-11-12 09:29, Khalid Al-Ghamdi schreef:
> Hi all,
> How would you go about adding items from a cursor to a dictionary?
There is a nice buitin way, with example here: 
http://docs.python.org/2/library/sqlite3.html#sqlite3.Row

It's not a real dictionary though, but it can act like it. Also, the 
docs say the following:
"""
If returning a tuple doesn’t suffice and you want name-based access to 
columns, you should consider setting row_factory to the highly-optimized 
sqlite3.Row type. Row provides both index-based and case-insensitive 
name-based access to columns with almost no memory overhead. It will 
probably be better than your own custom dictionary-based approach or 
even a db_row based solution.
"""

Timo

>
> i tried this but to no avail:
>
> >>> cur.execute('select * from schedule limit 10')
> <sqlite3.Cursor object at 0x0112CE60>
> >>> for i in range(len(cur.fetchall())):
> d[i]=cur.fetchall()[i]
>
> Traceback (most recent call last):
> File "<pyshell#70>", line 2, in <module>
> d[i]=cur.fetchall()[i]
> IndexError: list index out of range
>
> Thanks
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list