· 8 years ago · Dec 13, 2017, 05:58 PM
1database.py
2
3import sqlite3
4
5class Database(object):
6 """sqlite3 database class that holds testers jobs"""
7 DB_LOCATION = "/root/Documents/testerJobSearch/tester_db.sqlite"
8
9 def __init__(self):
10 """Initialize db class variables"""
11 self.connection = sqlite3.connect(Database.DB_LOCATION)
12 self.cur = self.connection.cursor()
13
14 def close(self):
15 """close sqlite3 connection"""
16 self.connection.close()
17
18 def execute(self, new_data):
19 """execute a row of data to current cursor"""
20 self.cur.execute(new_data)
21
22 def executemany(self, many_new_data):
23 """add many new data to database in one go"""
24 self.create_table()
25 self.cur.executemany('REPLACE INTO jobs VALUES(?, ?, ?, ?)', many_new_data)
26
27 def create_table(self):
28 """create a database table if it does not exist already"""
29 self.cur.execute('''CREATE TABLE IF NOT EXISTS jobs(title text,
30 job_id integer PRIMARY KEY,
31 company text,
32 age integer)''')
33
34 def commit(self):
35 """commit changes to database"""
36 self.connection.commit()