-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_driver.py
51 lines (45 loc) · 1.42 KB
/
database_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sqlite3
from sqlite3 import Error
class database:
def __init__(self, db_file):
self.db_file = db_file
def create_connection(self):
""" create a database connection to a SQLite database """
self.conn = None
try:
self.conn = sqlite3.connect(self.db_file)
return self.conn
except Error as e:
print(e)
return self.conn
def create_table(self, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
conn = self.create_connection()
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def execute_query(self, query_sql, query_params):
try:
conn = self.create_connection()
curs = conn.cursor()
curs.execute(query_sql, query_params)
conn.commit()
conn.close()
except Error as e:
print(e)
def select(self, query_sql, query_params = ''):
try:
conn = self.create_connection()
curs = conn.cursor()
curs.execute(query_sql, query_params)
rows = curs.fetchall()
conn.close()
return rows
except Error as e:
raise e