This repository was archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
112 lines (87 loc) · 3.12 KB
/
Copy pathdatabase.py
File metadata and controls
112 lines (87 loc) · 3.12 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sqlite3
import os
# Get relative path to the server folder where the database file is located
base_dir = os.path.dirname(os.path.abspath(__file__))
class Database:
"""
Class used to interact with sqlite database
"""
def __init__(self, db_name):
self.db = sqlite3.connect(f'{base_dir}/{db_name}')
def add_table(self, table_name, **columns):
"""
Adds a table in the database
:param table_name: Name of the table to be created.
:param **columns: Rest of the arguments in the following format: title="text", id="int", etc.
"""
self.cols = ""
for col_name, col_type in columns.items():
self.cols += col_name+" "+col_type+","
self.cols = self.cols[0:len(self.cols)-1]
self.db.execute("CREATE TABLE IF NOT EXISTS {}({})".format(
table_name, self.cols))
def drop_table(self, table_name):
self.db.execute("DROP TABLE IF EXISTS {}".format(
table_name))
def insert(self, table_name, *data):
"""
Inserts a row in a table
:param table_name: Name of the table to be used.
:param **data: Rest of the arguments which should be ordered like the columns in the table.
"""
self.data = ""
for value in data:
if value == "null":
self.data += ''+value.strip('"')+''+','
else:
self.data += '"'+value.replace('"', '')+'"'+','
self.data = self.data[0:len(self.data)-1]
self.db.execute("INSERT INTO {} values({})".format(
table_name, self.data))
self.db.commit()
def remove(self, table_name, where="1"):
"""
Remove a row in a table where a statement matches
:param table_name: Name of the table to be used.
:param where: Condition to be used for removal.
"""
self.where = where
self.db.execute("DELETE FROM {} WHERE {}".format(
table_name, self.where))
self.db.commit()
def drop(self, table_name):
self.db.execute("DROP TABLE IF EXISTS {}".format(
table_name))
self.db.commit()
def get_items(self, table_name, where=1):
"""
Get row(s) in table where condition is met
:param table_name: Name of the table to be used.
:param where: Condition to be used.
"""
if(table_name != 1):
self.where = where
self.items = self.db.execute(
"SELECT * FROM {} WHERE {}".format(table_name, self.where))
self.db.commit()
return list(self.items)
else:
return {}
def get_tables(self):
"""
Return list of tables
"""
self.tables = self.db.execute("SELECT name FROM sqlite_master")
return list(self.tables)
def query(self, query_string):
"""
Execute sqlite query
"""
self.results = self.db.execute(query_string)
self.db.commit()
return self.results
def close_connection(self):
"""
Close database connection
"""
self.db.close()