[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to use SQLite (sqlite3) more efficiently

ps16thypresenceisfullnessofjoy at gmail.com wrote:

> I'm completely new to SQL, and recently started using SQLite in one of my
> Python programs. I've gotten what I wanted to work, but I'm not sure if
> I'm doing it in the best/most efficient way. I have attached some sample
> code and would appreciate any (polite) comments about how the SQL (or
> Python) in it could be improved. The code is written in Python 2, but I
> think it should work in Python 3 if the 4 print statements are changed to
> function calls. Am I correct that the function 'set_description2' should
> work the same way as 'set_description'?

> def set_description2(conn, name, description):
>     with conn:
>         if description:
>             conn.execute("INSERT OR REPLACE INTO ProgrammingLanguages " \
>                 "VALUES(?,?)", (name, description))
>         else:
>             conn.execute("DELETE FROM ProgrammingLanguages WHERE Name=?",
>                 (name,))
>     conn.commit()

Have a look at these to see how this can be wrapped into a dict-like object: