Skip to content

Instantly share code, notes, and snippets.

@Phuket2
Created November 24, 2015 11:43
Show Gist options
  • Select an option

  • Save Phuket2/2247b89cac0c89da0830 to your computer and use it in GitHub Desktop.

Select an option

Save Phuket2/2247b89cac0c89da0830 to your computer and use it in GitHub Desktop.
Dicttest.py
# coding: utf-8
# coding: utf-8
from collections import OrderedDict
import sqlite3
from random import randint
from faker import Faker
fake = Faker()
db_def ={
'db_name': 'test.db',
# some other fields to come later...
'flds' : OrderedDict((('id','INTEGER PRIMARY KEY'),
('resid','INTEGER UNIQUE'),
('key','TEXT') ,
('ord','INTEGER'),
('value','INTEGER'),
('value1','TEXT'),
('data','TEXT'),
('pickled','INTEGER')))
}
# diervived from our db_def[field_names]
REC = OrderedDict((attr, None) for attr in db_def['flds'].keys())
_table_sql_new = '''CREATE TABLE IF NOT EXISTS '{0}' ({1})'''.format('{0}', ', '.join( '{0} {1}'.format(k, v) for k,v in db_def['flds'].items()))
insert_pattern = '({0})'.format(", ".join("?" * len(db_def['flds'])) )
_insert_sql = ''' INSERT INTO {0} VALUES ''' + insert_pattern
def new_record(**kwargs):
# not sure if i can do this better or not.
# create a empty record with all fields set to None
#rec= OrderedDict((attr, None) for attr in db_def['flds'].keys())
rec = OrderedDict(REC)
for k,v in kwargs.iteritems():
if rec.has_key(k):
rec[k] = v
return rec
def dict_factory(cursor, row):
#d = OrderedDict((attr, None) for attr in db_def['flds'].keys())
rec = OrderedDict(REC)
for idx, col in enumerate(cursor.description):
rec[col[0]] = row[idx]
return rec
if __name__ == '__main__':
# not that is really matters in this case, but because the id's
# are different, i have create a new copy of db_def because i
# called dict(db_def), if i just do mydb_def = db_def ids are the
# same. makes sense. Simple stuff but easy for us newbies to slip
# up on these small things.
mydb_def = dict(db_def)
print id(mydb_def), id(db_def)
db_name = mydb_def['db_name']
db_table = 'table_c'
recs_to_add = 2
conn = sqlite3.connect(db_name)
with conn:
conn.execute(_table_sql_new.format(db_table))
# using randint() for testing...resid is unquie
for i in range(1, recs_to_add):
rnd_resid = randint(1, 500000)
r = new_record(resid = rnd_resid, key = fake.city(), data = fake.first_name(), bad_keyword = 'bad info')
print r.values()[0]
conn.execute(_insert_sql.format(db_table), r.values())
conn.commit()
conn.row_factory = dict_factory
cur = conn.execute('SELECT * FROM {0}'.format(db_table))
for d in cur:
print d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment