From d242e4ff6860ebc05666157b5f6d17deebada8b1 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Tue, 7 Jan 2025 00:18:18 +0100 Subject: [PATCH] [main] Testing SqlAlchemy models --- src/__init__.py | 0 src/app.py | 68 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 13 deletions(-) delete mode 100644 src/__init__.py diff --git a/src/__init__.py b/src/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/app.py b/src/app.py index bb3cec7..f553d0e 100644 --- a/src/app.py +++ b/src/app.py @@ -1,14 +1,56 @@ -import sys -from PySide6.QtGui import QGuiApplication -from PySide6.QtQml import QQmlApplicationEngine +# import sys +# from PySide6.QtGui import QGuiApplication +# from PySide6.QtQml import QQmlApplicationEngine -if __name__ == "__main__": - app = QGuiApplication(sys.argv) - engine = QQmlApplicationEngine() - engine.addImportPath(sys.path[0]) - engine.loadFromModule("ui", "Main") - if not engine.rootObjects(): - sys.exit(-1) - exit_code = app.exec() - del engine - sys.exit(exit_code) \ No newline at end of file +# if __name__ == "__main__": +# app = QGuiApplication(sys.argv) +# engine = QQmlApplicationEngine() +# engine.addImportPath(sys.path[0]) +# engine.loadFromModule("ui", "Main") +# if not engine.rootObjects(): +# sys.exit(-1) +# exit_code = app.exec() +# del engine +# sys.exit(exit_code) + + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from models.book import Book, BookStatusEnum +from models.author import Author + +# Replace with your MySQL database credentials +DATABASE_URI = 'mysql+mysqlconnector://username:password@localhost:3306/library' + +# Create the engine +engine = create_engine(DATABASE_URI, echo=True) + +# Create a configured session class +SessionLocal = sessionmaker(bind=engine) + +# Create a session instance +session = SessionLocal() + +new_author = Author(first_name="John", last_name="Doe") + +session.add(new_author) +session.commit() + +new_book = Book( + title="Sample Book", + description="A fascinating tale.", + year_published="2023", + isbn="1234567890123", + status='available', + author_id=new_author.id +) + +session.add(new_book) +session.commit() + +books = session.query(Book).all() +for book in books: + print(book.title, book.author.first_name) + +session.close() \ No newline at end of file