fapi/app/database.py

44 lines
1.2 KiB
Python

#%%
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
#SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # Dla SQLite
SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.18.0.2:3306/test"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
#connect_args={"check_same_thread": False} # Opcja specyficzna dla SQLite
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
#%%
from sqlalchemy import text
with engine.connect() as conn:
result = conn.execute(text("select 'hello world'"))
print(result.all())
#%%
# Tworzenie tabeli w bazie danych
Base.metadata.create_all(bind=engine)
#%%
#with engine.connect() as conn:
# result = conn.execute(text("SELECT FirstName, LastName FROM Persons"))
# for row in result:
# print(f"FirstName: {row.FirstName} LastName: {row.LastName}")
#%%
"""
with engine.connect() as conn:
conn.execute(
text("INSERT INTO Persons (FirstName, LastName) VALUES (:fn, :ln)"),
[{"fn": "Sz", "ln": "Ptak"}, {"fn": "Szpili", "ln": "Sz"}],
)
conn.commit()
"""