-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_database.py
More file actions
28 lines (24 loc) · 1013 Bytes
/
query_database.py
File metadata and controls
28 lines (24 loc) · 1013 Bytes
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
import mysql.connector
from dotenv import load_dotenv
import os
load_dotenv()
azure_mysql_username = os.getenv("AZURE_MYSQL_USERNAME")
azure_mysql_password = os.getenv('AZURE_MYSQL_PASSWORD')
azure_mysql_host = os.getenv("AZURE_MYSQL_HOST")
azure_mysql_port = 3306
azure_mysql_database = os.getenv("AZURE_MYSQL_DATABASE")
# Function to fetch data from MySQL database
def fetch_data(query):
# Connect to MySQL database
connection = mysql.connector.connect(user=azure_mysql_username,
password=azure_mysql_password,
host=azure_mysql_host,
port=azure_mysql_port,
database=azure_mysql_database,
ssl_disabled=True)
cursor = connection.cursor()
cursor.execute(query)
columns = [column[0] for column in cursor.description] # Get column names
data = cursor.fetchall() # Get data
cursor.close()
return columns, data