mirror of
https://github.com/kristoferssolo/LU-bookstore.git
synced 2025-10-21 18:00:34 +00:00
style(sql): format sql create
This commit is contained in:
parent
15112f912e
commit
00adf20008
@ -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.")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user