What is the recommended python module for SQL database access?

Asaf Las roegltd at gmail.com
Sun Feb 9 08:27:01 EST 2014


On Sunday, February 9, 2014 3:14:50 PM UTC+2, Chris Angelico wrote:
> On Sun, Feb 9, 2014 at 11:47 PM, Asaf Las <r... at gmail.com> wrote:
> 

Thanks

> 
> Also, you're connecting and disconnecting repeatedly... oh, I see why
> it didn't work when I tried. You're also using two completely
> different database names: 'messageid.db' which is named in a constant
> and in the default argument, and 'example.db' which is what you
> actually use. Should have a single source of truth, otherwise you
> confuse the people who might otherwise be able to test your code :)

my apologies , it was deep night, when i got disappointed and forget to 
update names so left as it is and did not check when posted :-) 

> Anyway. This code is extremely inefficient:
> 
> >        conn = sqlite3.connect('example.db', 10.0, True, "EXCLUSIVE")
> >        c = conn.cursor()
> >        c.execute("SELECT val FROM msgid WHERE id = 0")
> >        tint = int(c.fetchone()[0]) + 1
> >        c.execute("UPDATE msgid SET val={0} WHERE id = 0".format(tint))
> >        conn.commit()
> >        conn.close()
> 
> Much more common would be to retain a connection and repeatedly
> perform queries. Then you won't need to open it EXCLUSIVE, and you can
> simply query, update, and then commit (all your locks should be
> released at the commit). Do that, and you should see at least
> reasonable performance, plus everything should work correctly.
> ChrisA

i did it just to test sqlite3 behavior and actually test was related to
simulation of  unique incremental sequence number/counter for 
independently spawned tasks accessing counter in non deterministic manner. 

/Asaf



More information about the Python-list mailing list