[main] Testing SqlAlchemy models
This commit is contained in:
parent
76465636ee
commit
d242e4ff68
68
src/app.py
68
src/app.py
@ -1,14 +1,56 @@
|
|||||||
import sys
|
# import sys
|
||||||
from PySide6.QtGui import QGuiApplication
|
# from PySide6.QtGui import QGuiApplication
|
||||||
from PySide6.QtQml import QQmlApplicationEngine
|
# from PySide6.QtQml import QQmlApplicationEngine
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
app = QGuiApplication(sys.argv)
|
# app = QGuiApplication(sys.argv)
|
||||||
engine = QQmlApplicationEngine()
|
# engine = QQmlApplicationEngine()
|
||||||
engine.addImportPath(sys.path[0])
|
# engine.addImportPath(sys.path[0])
|
||||||
engine.loadFromModule("ui", "Main")
|
# engine.loadFromModule("ui", "Main")
|
||||||
if not engine.rootObjects():
|
# if not engine.rootObjects():
|
||||||
sys.exit(-1)
|
# sys.exit(-1)
|
||||||
exit_code = app.exec()
|
# exit_code = app.exec()
|
||||||
del engine
|
# del engine
|
||||||
sys.exit(exit_code)
|
# 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()
|
Loading…
x
Reference in New Issue
Block a user