# The sqlite3 lets you create and use # a database with just a file import sqlite3 # more detailed python doc sqlite3 import os # in this example we get the current working dir path # Choose the file to use for the # db and connect (create it) conn = sqlite3.connect(os.path.abspath('.') + "tempdb") # grab a cursor and we can create the db schema c = conn.cursor() # if you happen to run through this example a few times # you may notice that the data is persistant. For this example # we'll ensure that we're starting from ground zero # drop the database (if it exists) c.execute('drop table if exists users') # create a table c.execute('create table users (name text, age text, email text)') # insert data c.execute("""insert into users values ('steve', '30', 'blah@blah.com')""") c.execute("""insert into users values ('steve2', '32', 'blah@blah2.com')""") c.execute("""insert into users values ('steve3', '33', 'blah@blah3.com')""") #, # ('steve II', '20', 'blah2@blah.com'), # ('steve III', '10', 'blah3@blah.com')""") # now lets select our data c.execute('select * from users') # iterate through the results with for each for row in c: print row # output: # (u'steve', u'30', u'blah@blah.com') # (u'steve2', u'32', u'blah@blah2.com') # (u'steve3', u'33', u'blah@blah3.com')
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Saturday, October 3, 2009
Python - using sqlite3 module for persistant data
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment