(in memory) database

Zentrader zentraders at gmail.com
Mon Sep 1 22:46:05 EDT 2008


On Ubuntu you want to install something like python-sqlite (a search
for "python" should turn up everything).  There are 2 parts to this,
SQLite and the python bindings to SQLite.  So you seem to have SQLite
installed but not the Python bindings.  Also, on some systems you have
to have python-sqlite installed, and then build python in order to use
it.  I'm on Gentoo so I can't help you with Ubuntu, but here's
something I had lying around that uses a DB in memory (no idea why I
did this or if it makes sense but you're welcome to it
import sqlite3 as sqlite

.##----------------------------------------------------------------------
.def add_rec(cur, con, add_tuple):
.   print "add_rec", len(add_tuple)
.   cur.execute("insert into test_it values
(?, ?, ?, ?, ?, ?, ?, ?, ?)", add_tuple)
.   con.commit()
.
.##----------------------------------------------------------------------
.def print_all_recs(cur):
.   # Execute the SELECT statement:
.   print "Printing all recs"
.   cur.execute("select * from test_it")
.
.   # Retrieve all rows as a sequence and print that sequence:
.   recs_list = cur.fetchall()
.   for rec in recs_list:
.      print rec
.      print "     stage, REAC_W =", rec[1], rec[5]
.
.##----------------------------------------------------------------------
.def add_test_data(cur, con):
.   #                   Stage     REAC_PS  WR(T)/P    TTO
REAC_W    W0       0wR(THCR)E/D  PTO
.   data_list=[ ('2.0', 'Stage1', '0.509',  '4.3010', '1602.30',
'0.515', '3.191',   '2.8191',   '29.7010'), \
.               ('2.0', 'Stage2', '0.488',  '6.0074', '1470.43',
'0.500', '3.200',   '3.9309',   '20.4275'), \
.               ('2.0', 'Stage1', '0.524',  '4.4623', '1602.30',
'0.560', '3.311',   '2.9243',   '29.7010'), \
.               ('2.0', 'Stage2', '0.579',  '6.6682', '1444.78',
'0.700', '3.320',   '4.3593',   '18.9262'), \
.               ('3.0', 'Stage1', '0.524',  '4.4623', '1602.30',
'0.560', '3.311',   '2.9243',   '29.7010'), \
.               ('3.0', 'Stage2', '0.579',  '6.6682', '1444.78',
'0.700', '3.320',   '4.3593',   '18.9262'), \
.               ('3.5', 'Stage1', '0.525',  '4.4695', '1602.30',
'0.563', '3.316',   '2.9290',   '29.7010') ]
.
.   for data_tuple in data_list :
.      add_rec(cur, con, data_tuple)
.
.##----------------------------------------------------------------------
.if __name__ == "__main__":
.   # Create a connection to the (memory) database file
.   con = sqlite.connect(':memory:')
.
.   # Get a Cursor object that operates in the context of Connection
con
.   cur = con.cursor()
.
.   cur.execute("CREATE TABLE test_it (number varchar, stage varchar,
REAC_PS varchar, WR_T_P varchar, TTO varchar, REAC_W varchar, W0
varchar, wR_THCR_E_D varchar, PTO varchar)")
.
.   add_test_data(cur, con)
.   print_all_recs(cur)
.
   .##----------------------------------------------------------------------
.   print '\n-----SELECT * FROM test_it where number="2.0"-------'
.   recs_list=cur.execute('SELECT * FROM test_it where number="2.0"')
.   ctr=0
.   for row in recs_list:
.      print row
.      ctr += 1
.   print ctr, "recs found"
.
.   print '\n-----SELECT * FROM test_it where number="2.0" and
stage="Stage1"'
.   lookup_dic={"dic_num":"2.0", "dic_st":"Stage1"}
.   recs_list=cur.execute('SELECT * FROM test_it where number=:dic_num
and stage=:dic_st', \
.	                  lookup_dic)
.   ctr=0
.   for row in recs_list:
.      print row
.      ctr += 1
.   print ctr, "recs found"



More information about the Python-list mailing list