This is a very simple example of how you could use SQLite in memory. This may be a case of the difficult way actually being the simpliest. Note that this is just a simple/quick example. There is a lot more on the web. If this is of some help you can thank The Blues. I was looking for something that I could do while listening to Bad Dog Blues.
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"