-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
42 lines (32 loc) · 1.19 KB
/
Copy pathdatabase.py
File metadata and controls
42 lines (32 loc) · 1.19 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
import mysql.connector
from filefunctions import read_database_config
import streamlit as st
def query_db(query):
# Establish a connection to the database
try:
# Accessing database information
config = read_database_config('database.yaml')
connection = mysql.connector.connect(
host=config['database']['host'],
user=config['database']['user'],
password=config['database']['password'],
database=config['database']['database']
)
print("Connected to the database")
# Create a cursor object to execute SQL queries
cursor = connection.cursor()
if connection.is_connected() and query != "":
# Execute the query
cursor.execute(query)
# Fetch all rows
rows = cursor.fetchall()
# Close the connection
if connection is not None and connection.is_connected():
cursor.close()
connection.close()
print("Connection closed")
return rows
except mysql.connector.Error as e:
st.write("Error connecting to the database:", e)
finally:
print("Done")