This repository was archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseTest.py
More file actions
93 lines (77 loc) · 3.01 KB
/
DatabaseTest.py
File metadata and controls
93 lines (77 loc) · 3.01 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# DatabaseTest.py
# Alexander Gotsis + agotsis + EE
# This is original code. Demonstrating understanding of databases.
import sqlite3
class DB(object):
def __init__(self, name):
self.db = sqlite3.connect('data/%s' % name) #create/connect to database
self.cursor = self.db.cursor() #everything with database uses cursor
self.db.commit() #MUST commit changes! to db
self.debug = False #set to True to print debugging info
def createTable(self, name): #overwrites tables!
self.cursor.execute("DROP TABLE IF EXISTS %s" % name) #remove if exists
self.cursor.execute('''
CREATE TABLE %s(id INTEGER PRIMARY KEY, name TEXT,
location TEXT,
checkedOut, INTEGER)''' % name) #All sqlite functions use execute
def createUsers(self):
self.cursor.execute("DROP TABLE IF EXISTS %s" % "users")#remove existing
self.cursor.execute('''
CREATE TABLE %s(id integer PRIMARY KEY, andrewid TEXT, name TEXT)
''' % "users") #All sqlite functions use execute
def addToDB(self, name, location, checkedOut):
#to be imporoved with *args so that anything can be added
self.cursor.execute('''INSERT INTO items(name, location, checkedOut)
VALUES(?,?,?)''', (name, location, checkedOut))
try:
self.db.commit()
except:
print("insert failed!")
if self.debug:
print("insert successful!")
def addUser(self, andID):
self.cursor.execute('''INSERT INTO users(name, andrewid)
VALUES(?,?)''', ("",andID))
try:
self.db.commit() #makes the actual changes to database
except:
print("insert failed!")
if self.debug:
print("insert successful!")
def get(self, command):
self.cursor.execute(command)
return self.cursor.fetchall()
def update(self, command):
self.cursor.execute(command)
try:
self.db.commit()
except:
print("update failed!")
if self.debug:
print("update successful!")
def delete(self, ID):
self.cursor.execute('''DELETE FROM items WHERE id = ? ''', (ID,))
try:
self.db.commit()
except:
print("delete failed!")
if self.debug:
print("delete successful!")
if __name__ == "__main__":
db = DB("DemonstrationDB")
# db.createTable("items") #uncomment to clear and recreate items table
# db.createUsers()
# db.addUser("agotsis")
# db.addUser("kdodhia")
#some test items to begin with
# db.addToDB("Quadbox", "Tech 1", True)
# db.addToDB("Quadbox", "Tech 1", False)
# db.addToDB("Quadbox", "Tech 2", True)
# db.addToDB("SOCA Case", "Tech 2", False)
# db.addToDB("GrandMA 2", "Tech 1", False)
print(db.get("""
select id, name, location from items
where location = "Tech 1"
"""))
print(db.get("""
select id, andrewid, name from users"""))