2024-10-23 12:20:21 +00:00
|
|
|
#%%
|
|
|
|
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
|
2024-10-25 12:56:44 +00:00
|
|
|
SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.18.0.4:3306/test"
|
2024-10-23 12:20:21 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
"""
|