-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
58 lines (43 loc) · 1.57 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
import mysql.connector as mysql
INSERT_ACCOUNT = "INSERT INTO accounts (name, username, password) VALUES (%s, %s, %s, %s)"
class AccountDatabase:
def __init__(self, host, name, password):
self.db = mysql.connect(
host=host,
user=name,
password=password)
self.query = ''
self.cursor = self.db.cursor()
self.values = None
def set_query(self, query):
self.query = query
def get_query(self):
return self.query
def create_database(self, database_name):
self.cursor.execute(f"DROP DATABASE {database_name};")
self.cursor.execute(f"CREATE DATABASE {database_name}")
self.cursor.execute(f"USE {database_name};")
def create_table(self):
# TODO: Revisit this
self.cursor.execute(self.query)
def get_values(self):
return self.values
def set_values(self, values):
self.values = values
def select_all_from_table(self, table_name):
self.cursor.execute(f"SELECT * FROM {table_name}")
results = self.cursor.fetchall()
for result in results:
print(result)
def custom_command(self, query):
self.cursor.execute(f"{query}")
results = self.cursor.fetchall()
for result in results:
return result
def insert_record(self, query):
self.cursor.execute(f"{query}")
self.db.commit()
def insert_many_records(self):
self.cursor.executemany(self.query, self.values)
self.db.commit()
print(f"{self.cursor.rowcount} tuples inserted")