-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
68 lines (59 loc) · 2.75 KB
/
database.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sqlite3
from sqlite3 import Error
class DBconnection:
def __init__(self):
self.database = "LMS.db"
self.sql_create_students_table = """ CREATE TABLE IF NOT EXISTS students (
sname text,
phone text,
fname text,
student_id text NOT NULL PRIMARY KEY
); """
self.sql_create_books_table = """ CREATE TABLE IF NOT EXISTS books (
bname text,
aname text,
category text,
book_id text NOT NULL PRIMARY KEY
); """
self.sql_create_issue_book_table = """ CREATE TABLE IF NOT EXISTS issued (
book_id text PRIMARY KEY,
student_id text,
FOREIGN KEY(book_id) REFERENCES book(book_id)
FOREIGN KEY(student_id) REFERENCES book(student_id)
); """
# create a database connection
self.conn = self.create_connection(self.database)
# create tables
if self.conn is not None:
# create projects table
self.create_table(self.conn, self.sql_create_students_table)
self.create_table(self.conn, self.sql_create_books_table)
self.create_table(self.conn, self.sql_create_issue_book_table)
else:
print("Error! cannot create the database connection.")
@staticmethod
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
@staticmethod
def create_table(conn, 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:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)