From 282bd73d422db947770781c9034c0ef8770be940 Mon Sep 17 00:00:00 2001 From: mpabi Date: Sun, 16 Jun 2024 14:58:39 +0000 Subject: [PATCH] init proj --- app/database.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 app/database.py diff --git a/app/database.py b/app/database.py new file mode 100644 index 0000000..e67e172 --- /dev/null +++ b/app/database.py @@ -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()