Loading SQLite3 values into Python variables -


i have project built in python 2.7, using sqlite3 database. need know how load item particular row , column existing python variable.

ty!

here basic steps:

import sqlite3 conn = sqlite3.connect(':memory:') curs = conn.cursor() results = curs.execute( """select mycol                               mytable                              somecol = ?;""", (some_var,) ).fetchall() curs.close() conn.close() 

for further research can using context manager (with statement), , how fetch results dict. here's example:

with sqlite3.connect(':memory:') conn:     curs = conn.cursor()     curs.row_factory = sqlite3.row     try:         results = curs.execute( """select mycol                                       mytable                                      somecol = ?;""",                                 (some_var,) ).fetchall()     # put exception-handling code here     finally:         curs.close() 

the benefits of context manager many, including fact connection closed you. benefit of mapping results in dict can access column values name opposed less-meaningful integer.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

c# - SharpSVN - How to get the previous revision? -

php cli reading files and how to fix it? -