-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
93 lines (75 loc) · 2.17 KB
/
database.py
File metadata and controls
93 lines (75 loc) · 2.17 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
import sqlite3
class Database:
def __init__(self):
#I know I don't have to initialize variables, but it helps me keep track of them
self.connection = ""
self.dbpath = ""
self.connected = False
def is_number(self, string):
try:
float(string)
return True
except ValueError:
return False
def GetDatabasePath(self):
return self.dbpath
def IsConnected(self):
return self.connected
def Connect(self, path):
#Establishes the connection to the database, checking that appropriate flags have
# been set. Returns "False" if the path variable does not existor if a connection
# already exists
if self.connected:
return False
self.dbpath = path
self.connection = sqlite3.connect(self.dbpath)
self.connected = True
return True
def Reconnect(self):
if self.connected:
return False
self.connection = sqlite3.connect(self.dbpath)
self.connected = True
return True
def Disconnect(self):
#Disconnects from the database, first checking that it's connected to a database.
# Returns "False" if not connected to a database
if self.connected:
self.connection.commit()
self.connection.close()
self.connected = False
self.dbpath = ""
return True
else:
return False
def Commit(self):
#Forces commitment
self.connection.commit()
def QueryDatabase(self, Query):
#Queries the database and returns the result
if self.connected:
cursor = self.connection.execute(Query)
return cursor
def ExecuteCommand(self, command):
if self.connected:
self.connection.execute(command)
def CreateTable(self, TableName, TableFields, TableTypes):
command = "CREATE TABLE " + TableName + "\n("
i = 0
for properT in TableFields:
command += properT + "\t" + TableTypes[i] + "," + "\n"
i += 1
command = command[:-2]
command += ");"
self.connection.execute(command)
self.connection.commit()
def InsertRow(self, TableName, RowValues):
command = "INSERT INTO " + TableName + " VALUES ("
for value in RowValues:
if self.is_number(value):
command += value + ","
else:
command += "'" + value + "',"
command = command[:-1]
command += ")"
self.connection.execute(command)