style(sql): format sql create

This commit is contained in:
Kristofers Solo 2023-10-24 11:29:54 +03:00
parent 15112f912e
commit 00adf20008

View File

@ -9,7 +9,17 @@ class Inventory:
def __init__(self, db_path: Path) -> None: def __init__(self, db_path: Path) -> None:
self.conn = sqlite3.connect(db_path) self.conn = sqlite3.connect(db_path)
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS Book (title TEXT, author TEXT, isbn TEXT PRIMARY KEY, price REAL, stock INTEGER)") self.cursor.execute(
r"""
CREATE TABLE IF NOT EXISTS Book (
isbn TEXT PRIMARY KEY NOT NULL,
title TEXT DEFAULT 'Unknown' NOT NULL,
author TEXT DEFAULT 'Unknown' NOT NULL ,
price REAL DEFAULT 0 NOT NULL,
stock INTEGER DEFAULT 0 NOT NULL
)
"""
)
def save(self) -> None: def save(self) -> None:
"""Save `Inventory` to SQLite database.""" """Save `Inventory` to SQLite database."""
@ -23,7 +33,7 @@ class Inventory:
"""Add `Book` to the `Inventory`. `Book`s ISBN must be unique.""" """Add `Book` to the `Inventory`. `Book`s ISBN must be unique."""
for book in books: for book in books:
try: try:
self.cursor.execute("INSERT INTO Book VALUES (?, ?, ?, ?, ?)", (book.title, book.author, book.isbn, book.price, book.stock)) self.cursor.execute("INSERT INTO Book VALUES (?, ?, ?, ?, ?)", (book.isbn, book.title, book.author, book.price, book.stock))
self.conn.commit() self.conn.commit()
except sqlite3.InternalError: except sqlite3.InternalError:
print(f"A book with ISBN {book.isbn} already exists in the database.") print(f"A book with ISBN {book.isbn} already exists in the database.")