From 00adf2000828d71a7fe31fbb5c5a5c0953f7d2fe Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 24 Oct 2023 11:29:54 +0300 Subject: [PATCH] style(sql): format sql create --- src/bookstore/inventory.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/bookstore/inventory.py b/src/bookstore/inventory.py index 31c080d..f218dec 100644 --- a/src/bookstore/inventory.py +++ b/src/bookstore/inventory.py @@ -9,7 +9,17 @@ class Inventory: def __init__(self, db_path: Path) -> None: self.conn = sqlite3.connect(db_path) 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: """Save `Inventory` to SQLite database.""" @@ -23,7 +33,7 @@ class Inventory: """Add `Book` to the `Inventory`. `Book`s ISBN must be unique.""" for book in books: 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() except sqlite3.InternalError: print(f"A book with ISBN {book.isbn} already exists in the database.")