[main] Testing SqlAlchemy models

This commit is contained in:
Thastertyn 2025-01-07 00:18:18 +01:00
parent 76465636ee
commit d242e4ff68
2 changed files with 55 additions and 13 deletions

View File

View File

@ -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)
# 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()