Skip to content

Commit

Permalink
Tracking with DB conns
Browse files Browse the repository at this point in the history
  • Loading branch information
nmagee committed Nov 19, 2024
1 parent e842eb6 commit 39b9d69
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
from mysql.connector import Error
import json
import os
from mysql.connector import RefreshOption
# from mysql.connector import RefreshOption

refresh = RefreshOption.LOG
# refresh = RefreshOption.LOG
# refresh = RefreshOption.LOG | RefreshOption.THREADS

DBHOST = "ds2022.cqee4iwdcaph.us-east-1.rds.amazonaws.com"
DBUSER = "ds2022"
DBUSER = "admin"
DBPASS = os.getenv('DBPASS')
DB = "nem2p"

db = mysql.connector.connect(user=DBUSER, host=DBHOST, password=DBPASS, database=DB, ssl_disabled=True)
cur=db.cursor()
# db = mysql.connector.connect(user=DBUSER, host=DBHOST, password=DBPASS, database=DB, ssl_disabled=True)
# cur=db.cursor()

app = FastAPI()

Expand All @@ -36,7 +36,9 @@ def zone_apex():

@app.get('/genres')
async def get_genres():
db.cmd_refresh(refresh)
# db.cmd_refresh(refresh)
db = mysql.connector.connect(user=DBUSER, host=DBHOST, password=DBPASS, database=DB, ssl_disabled=True)
cur = db.cursor()
query = "SELECT * FROM genres ORDER BY genreid;"
try:
cur.execute(query)
Expand All @@ -45,23 +47,33 @@ async def get_genres():
json_data=[]
for result in results:
json_data.append(dict(zip(headers,result)))
cur.close()
db.close()
return(json_data)
except Error as e:
print("MySQL Error: ", str(e))
cur.close()
db.close()
return {"Error": "MySQL Error: " + str(e)}

@app.get('/songs')
async def get_genres():
db.cmd_refresh(refresh)
# db.cmd_refresh(refresh)
db = mysql.connector.connect(user=DBUSER, host=DBHOST, password=DBPASS, database=DB, ssl_disabled=True)
cur = db.cursor()
query = "SELECT songs.title, songs.album, songs.artist, songs.year, songs.file, songs.image, genres.genre FROM songs JOIN genres WHERE songs.genre = genres.genreid;"
try:
try:
cur.execute(query)
headers=[x[0] for x in cur.description]
results = cur.fetchall()
json_data=[]
for result in results:
json_data.append(dict(zip(headers,result)))
cur.close()
db.close()
return(json_data)
except Error as e:
print("MySQL Error: ", str(e))
cur.close()
db.close()
return None

0 comments on commit 39b9d69

Please sign in to comment.