4a and 4b Database Hacks
import sqlite3
def create_connection(db):
connection = None
try:
connection = sqlite3.connect(db)
return connection
except Error as e:
print(e)
return connection
def create_table(connection, createTblSql):
try:
cursor = connection.cursor()
cursor.execute(createTblSql)
except Error as e:
print(e)
def main():
database = 'instance/books.db'
createTblSql = """ CREATE TABLE IF NOT EXISTS books (
_id integer PRIMARY KEY,
_title text NOT NULL,
_author text NOT NULL,
_genre text NOT NULL,
_review text NOT NULL
); """
connection = create_connection(database)
# create books table
if connection is not None:
create_table(connection, createTblSql)
else:
print('Connection Error')
if __name__ == '__main__':
main()
def create():
database = 'instance/books.db'
title = input("Enter the book title")
author = input("Enter book author name")
genre = input("Enter book genre")
review = input("Enter review")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
# Execute SQL to insert record in to db
cursor.execute("INSERT INTO books (_title, _author, _genre, _review) VALUES (?, ?, ?, ?)", (title, author, genre, review))
# Commit the changes
connection.commit()
print(f"New book with {title} is added.")
except sqlite3.Error as error:
print("Error while inserting record", error)
# Closing cursor and connection
cursor.close()
connection.close()
create()
def read():
database = 'instance/books.db'
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
# Fetch all the records from books table
results = cursor.execute('SELECT * FROM books').fetchall()
if len(results) != 0:
for row in results:
print(row)
else:
print("No books")
# Closing cursor and connection
cursor.close()
connection.close()
read()
import sqlite3
# updating review
def update():
database = 'instance/books.db'
bookId = input("Enter a book id to update the review")
review = input("Enter new review")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
# Updating review for the book
cursor.execute("UPDATE books SET _review = ? WHERE _id = ?", (review, bookId))
if cursor.rowcount != 0:
print(f"Review for the book is updated to {review}")
connection.commit()
else:
print(f"Book not found")
except sqlite3.Error as error:
print("Error occurred", error)
# Closing cursor and connection
cursor.close()
connection.close()
update()
import sqlite3
def delete():
database = 'instance/books.db'
bookId = input("Enter Book id to delete")
# Connecting to the database, create cursor to execute the SQL command
connection = sqlite3.connect(database)
cursor = connection.cursor()
try:
cursor.execute("DELETE FROM books WHERE _id = ?", (bookId,))
if cursor.rowcount == 0:
print(f"{bookId} does not exist")
else:
print(f"Successfully deleted book with id {bookId}")
connection.commit()
except sqlite3.Error as error:
print("Error occurred: ", error)
# Closing cursor and connection
cursor.close()
connection.close()
delete()