forked from ItsKoga/AutoVox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
62 lines (55 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
59
60
61
62
import mysql.connector
from mysql.connector import Error
import os
def create_connection():
# Create a connection to the MySQL database
connection = None
try:
connection = mysql.connector.connect(
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
passwd=os.getenv('DB_PASS'),
database=os.getenv('DB_NAME')
)
except Error as e:
return e
return connection
def execute_query(query, values=None):
connection = create_connection()
# Execute a query on the MySQL database
cursor = connection.cursor()
try:
cursor.execute(query, values)
connection.commit()
close_connection(connection)
return True
except Error as e:
close_connection(connection)
return e
def execute_read_query(query):
connection = create_connection()
# Execute a read query on the MySQL database
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
close_connection(connection)
return result
except Error as e:
close_connection(connection)
return e
def check_database():
connection = create_connection()
# Check if the database is connected
try:
if connection.is_connected():
close_connection(connection)
return True
else:
return connection
except:
return False
def close_connection(connection):
# Close the connection to the MySQL database
connection.close()