===== list all tables/indices contained in an SQLite database SELECT name FROM sqlite_master WHERE type='table' ORDER BY name; ===== Never do this -- insecure! symbol = 'IBM' c.execute("... where symbol = '%s'" % symbol) # Do this instead t = (symbol,) c.execute('select * from stocks where symbol=?', t) # Larger example for t in (('2006-03-28', 'BUY', 'IBM', 1000, 45.00), ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00), ('2006-04-06', 'SELL', 'IBM', 500, 53.00), ): c.execute('insert into stocks values (?,?,?,?,?)', t) ===== c = conn.cursor() c.execute('select * from stocks order by price') for row in c: print row ===== from pysqlite2 import dbapi2 as sqlite3 class Point(object): def __init__(self, x, y): self.x, self.y = x, y def __conform__(self, protocol): if protocol is sqlite3.PrepareProtocol: return "%f;%f" % (self.x, self.y) con = sqlite3.connect(":memory:") cur = con.cursor() p = Point(4.0, -3.2) cur.execute("select ?", (p,)) print cur.fetchone()[0] ===== from pysqlite2 import dbapi2 as sqlite3 import datetime con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) cur = con.cursor() cur.execute("create table test(d date, ts timestamp)") today = datetime.date.today() now = datetime.datetime.now() cur.execute("insert into test(d, ts) values (?, ?)", (today, now)) cur.execute("select d, ts from test") row = cur.fetchone() print today, "=>", row[0], type(row[0]) print now, "=>", row[1], type(row[1]) cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"') row = cur.fetchone() print "current_date", row[0], type(row[0]) print "current_timestamp", row[1], type(row[1]) ===== from pysqlite2 import dbapi2 as sqlite3 persons = [ ("Hugo", "Boss"), ("Calvin", "Klein") ] con = sqlite3.connect(":memory:") # Create the table con.execute("create table person(firstname, lastname)") # Fill the table con.executemany("insert into person(firstname, lastname) values (?, ?)", persons) # Print the table contents for row in con.execute("select firstname, lastname from person"): print row # Using a dummy WHERE clause to not let SQLite take the shortcut table deletes. print "I just deleted", con.execute("delete from person where 1=1").rowcount, "rows" ===== Rows wrapped with this class can be accessed both by index (like tuples) and case-insensitively by name: from pysqlite2 import dbapi2 as sqlite3 con = sqlite3.connect("mydb") con.row_factory = sqlite3.Row cur = con.cursor() cur.execute("select name_last, age from people") for row in cur: assert row[0] == row["name_last"] assert row["name_last"] == row["nAmE_lAsT"] assert row[1] == row["age"] assert row[1] == row["AgE"] ===== from pysqlite2 import dbapi2 as sqlite import re # Return 1 if `item` matches the regular expression `expr`, 0 otherwise def regexp(expr, item): r = re.compile(expr) return r.match(item) is not None con = sqlite.connect(":memory:") # Create the function "regexp" for the REGEXP operator of SQLite con.create_function("regexp", 2, regexp) cur = con.cursor() # Create schema and test data cur.execute("create table test(foo)") cur.execute("insert into test(foo) values ('aaxaa')") cur.execute("insert into test(foo) values ('aayaa')") # Two demonstrations print "1 --------------------" cur.execute("select foo from test where foo regexp '.*x.*'") print cur.fetchall() print "2 --------------------" cur.execute("select foo from test where foo regexp '[aA]'") print cur.fetchall() There is a bug in the above code that doesn't work with pysqlite 2.0.3 (haven't tested on other versions), but you need to do this instead. Just replace and it works print "1 --------------------" #cur.execute("select foo from test where foo regexp '.*x.*'") cur.execute("select foo from test where regexp('.*x.*', foo)") print cur.fetchall() print "2 --------------------" #cur.execute("select foo from test where foo regexp '[aA]'") cur.execute("select foo from test where regexp('[aA]', foo)") print cur.fetchall() ===== from pysqlite2 import dbapi2 as sqlite import os blobdata = open('c:\\sqlite3\\img.jpg','rb').read() con = sqlite.connect(':memory:') cur = con.cursor() cur.execute("Create table picture_table(images)") cur.execute("Insert into picture_table(images) values (?)",(sqlite.Binary(blobdata),)) con.commit() cur.execute("select * from picture_table") returnImg = cur.fetchone() print returnImg (,) imgOut = open('C:\\imgOut.jpg','wb') imgOut.write(returnImg[0]) imgOut.close() cur.close() con.close() ===== ===== ===== =====