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

How to use SQLite (sqlite3) more efficiently

Sorry for the attachment issue. I'm used to the wxPython-users Google 
group, where posters are instructed to attach code to their post instead 
of including it in the body of the message.
I placed the latest version of my sample code below, since I made a few 
minor changes to it after posting it as that attachment. (I changed 
REPLACE to INSERT OR REPLACE as I mentioned above, followed Chris' 
suggestion to not use backslash continuation characters, and changed the 
description of C++ as mentioned below.)
FYI, you can also view the version (now outdated) that I had attached on 
the Google Groups website at:

 > > OK, I'll admit that I don't know Assembly :). How about the paradox
 > "Making
 > > Easy Things Hard & Hard Things Easy"? Although that might make my
 > > description of C++ too unfair; suggestions for improvements to my
 > language
 > > descriptions are welcome :).
 > Hehe. As I'm sure you're aware, this has absolutely nothing to do with
 > your SQL or Python code.

Of course :). It's just for fun. I changed the description for C++ to 
"Making Easy Things Hard & Hard Things Hard" (without the -er on the end).

 > (Suggestion: Always reply to an existing post if it's part of the same
 > thread. Replying to your own post is fine, and it links the thread
 > together nicely.)

Sorry about that. As you can probably tell, I'm relatively new to using 
mailing lists. I'm not exactly sure why that occurred like it did. I'll 
try adding "Re:" in front of the subject when I send this e-mail, and 
see if it works right this time. If not, I guess you'll have to please 
explain to me what I'm doing wrong.

Thank you again for your help.

-- Timothy

*** sqlite_test.py ***
from __future__ import print_function
import sqlite3

def get_description(conn, description):
     row = conn.execute("SELECT description FROM languages WHERE name=?",
     if row:
         return row[0]

def set_description(conn, name, description):
     conn.execute("INSERT OR REPLACE INTO languages VALUES(?,?)",
         (name, description))

def delete_language(conn, name):
     conn.execute("DELETE FROM languages WHERE name=?", (name,))

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE IF NOT EXISTS languages(name TEXT PRIMARY KEY, "
     "description TEXT NOT NULL)")
set_description(conn, "Perl", "Making Easy Things Easy & Hard Things 
set_description(conn, "Python", "Making Easy Things Easier & Hard Things 
set_description(conn, "C++", "Making Easy Things Hard & Hard Things Hard")
for language in ("Perl", "Python", "C++"):
     print("%s: %s" % (language, get_description(conn, language)))
set_description(conn, "Assembly", "Making Easy Things Very Hard & "
     "Hard Things Impossible (Hey, that's not fair!)")
print("Assembly: %s" % get_description(conn, "Assembly"))
set_description(conn, "Assembly",
     "Making Easy Things Hard & Hard Things Easy")
print("Assembly: %s" % get_description(conn, "Assembly"))  # Should be 
delete_language(conn, "Assembly")
print("Assembly: %s" % get_description(conn, "Assembly"))  # Should be None