Create Table
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/photos.db'
createTblSql = """ CREATE TABLE IF NOT EXISTS Games (
_id integer PRIMARY KEY,
_photo text NOT NULL,
_x integer NOT NULL,
_y integer NOT NULL,
_difficulty text NOT NULL
); """
connection = create_connection(database)
# create games table
if connection is not None:
create_table(connection, createTblSql)
else:
print('Connection Error')
if __name__ == '__main__':
main()
def create():
database = 'instance/games.db'
name = input("Enter your name")
age = input("Enter your age")
game = input("Enter your favorite game")
console = input("Enter the console you play on")
# 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 games (_name, _age, _game, _console) VALUES (?, ?, ?, ?)", (name, age, game, console))
# Commit the changes
connection.commit()
print(f"New game {game} is added to your database.")
except sqlite3.Error as error:
print("Error while inserting game", error)
# Closing cursor and connection
cursor.close()
connection.close()
create()
def read():
database = 'instance/games.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 games').fetchall()
if len(results) != 0:
for row in results:
print(row)
else:
print("No Games In Your Database")
# Closing cursor and connection
cursor.close()
connection.close()
read()
import sqlite3
# updating review
def update():
database = 'instance/games.db'
name = input("Enter your name to update your new favorite game")
game = input("Enter new game")
# 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 games SET _game = ? WHERE _name = ?", (game, name))
if cursor.rowcount != 0:
print(f"Review for the game is updated to {game}")
connection.commit()
else:
print(f"Name 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/games.db'
name = input("Enter FULL NAME 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 games WHERE _name = ?", (name,))
if cursor.rowcount == 0:
print(f"{name} does not exist")
else:
print(f"Successfully deleted game with name {name}")
connection.commit()
except sqlite3.Error as error:
print("Error occurred: ", error)
# Closing cursor and connection
cursor.close()
connection.close()
delete()