blob: d6b3f755d25693a43b1041905d9bfebcf8952f6b [file] [log] [blame]
import sqlite3
import sys
class SubscriberDB:
def __init__(self, db = 'subscriber.db', create = False):
self.db = db
self.con = sqlite3.connect(db)
self.con.row_factory = sqlite3.Row
self.cur = self.con.cursor()
self.services = [ 'DHCP', 'IGMP' ]
self.create = create
if create == True:
self.cur.execute("DROP TABLE IF EXISTS Subscriber")
self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);")
def load(self, name, service):
self.cur.execute("INSERT INTO Subscriber(Name, Service) VALUES (?, ?);", (name, service))
def commit(self):
self.con.commit()
def generate(self, num = 100):
#create db if not created
if self.create is False:
self.cur.execute("DROP TABLE IF EXISTS Subscriber")
self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);")
self.create = True
service = ' '.join(self.services)
for i in xrange(num):
name = "sub%d" %self.lastrowid()
self.load(name, service)
self.commit()
def read(self, num = 1000000, debug = False):
self.cur.execute("SELECT * FROM Subscriber LIMIT ?;", (num,))
rows = self.cur.fetchall()
if debug is True:
for row in rows:
print('Id %d, Name %s, Service %s' %(row['Id'], row['Name'], row['Service']))
return rows
def lastrowid(self):
return 0 if self.cur.lastrowid == None else self.cur.lastrowid
if __name__ == "__main__":
create = False
if len(sys.argv) > 1:
try:
num_subscribers = int(sys.argv[1])
except:
num_subscribers = 100
print('Creating %d subscriber records' %num_subscribers)
create = True
sub = SubscriberDB(create = create)
if create == True:
sub.generate(num_subscribers)
else:
num_subscribers = 10
subscribers = sub.read(num_subscribers)
for s in subscribers:
print('Name %s, Service %s' %(s['Name'], s['Service']))