init proj

This commit is contained in:
mpabi 2024-06-16 14:58:39 +00:00
commit 282bd73d42
1 changed files with 37 additions and 0 deletions

37
app/database.py Normal file
View File

@ -0,0 +1,37 @@
#%%
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
"""
#SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # SQLite
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} # Opcja dla SQLite
)
"""
#%%
SQLALCHEMY_DATABASE_URL = "mysql+mysqlconnector://root:secret@172.18.0.3:3306/mpabi" # Przykład dla MySQL
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
)
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())
#%%
with engine.connect() as conn:
conn.execute(text("CREATE TABLE some_table (x int, y int)"))
conn.execute(
text("INSERT INTO some_table (x, y) VALUES (:x, :y)"),
[{"x": 1, "y": 1}, {"x": 2, "y": 4}],
)
conn.commit()