-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
72 lines (61 loc) · 2.03 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
63
64
65
66
67
68
69
70
71
72
from flask import jsonify
from sqlalchemy import create_engine, text
from dotenv import load_dotenv
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import SQLAlchemyError
import os
load_dotenv()
db_host = os.getenv("DB_HOST")
db_port = os.getenv("DB_PORT")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_name = os.getenv("DB_NAME")
timeout = 10
engine = create_engine(f"mysql+pymysql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}")
def show_all_users():
with engine.connect() as conn:
result = conn.execute(
text(f"SELECT * FROM users")
)
rows = []
for row in result.all():
rows.append(row._mapping)
print(rows)
return rows
def load_rank_from_db(id):
try:
with engine.connect() as conn:
result = conn.execute(text("CALL getrank(:id)"), {'id': id})
row = result.fetchone()
return dict(row._mapping) if row else None
except SQLAlchemyError as e:
print(f"Error: {e}")
return None
def load_all_ranks_from_db():
try:
with engine.connect() as conn:
result = conn.execute(text("CALL getallranks()"))
rows = [row._mapping for row in result.all()]
return rows if rows else None
except SQLAlchemyError as e:
print(f"Error: {e}")
return None
def load_all_log_contents_from_db():
try:
with engine.connect() as conn:
result = conn.execute(text("select * from battle"))
rows = [row._mapping for row in result.all()]
return rows if rows else None
except SQLAlchemyError as e:
print(f"Error: {e}")
return None
def load_all_pokemon_cards():
try:
with engine.connect() as conn:
result=conn.execute(text("select Pokeid,name from pokemon order by name"))
rows=[row._mapping for row in result.all()]
return rows if rows else None
except SQLAlchemyError as e:
print(f"Error: {e}")
return None
# print(load_all_pokemon_cards())