forked from olmstea1/python-romscraper
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.6 KiB
94 lines
3.6 KiB
4 years ago
|
import sqlite3
|
||
|
from dbHandler import dbConnection
|
||
|
|
||
|
|
||
|
class Console:
|
||
|
console_id = 0
|
||
|
name = ""
|
||
|
description = ""
|
||
|
thumbnail_uri = ""
|
||
|
rating = 0
|
||
|
|
||
|
def __init__(self, console_id=None, name=None):
|
||
|
if console_id is None and name is not None:
|
||
|
self.name = name
|
||
|
try:
|
||
|
sqliteConnection = sqlite3.connect('Romscraper.db')
|
||
|
cursor = sqliteConnection.cursor()
|
||
|
# print("Connected")
|
||
|
query = "SELECT * FROM Consoles WHERE name=?"
|
||
|
cursor.execute(query, [name, ])
|
||
|
records = cursor.fetchall()
|
||
|
if len(records) < 1:
|
||
|
# print("Console is not in table")
|
||
|
query = "INSERT INTO Consoles (name) VALUES (?)"
|
||
|
cursor.execute(query, [name, ])
|
||
|
sqliteConnection.commit()
|
||
|
query = "SELECT seq FROM sqlite_sequence WHERE name=?"
|
||
|
cursor.execute(query, ["Consoles"])
|
||
|
records = cursor.fetchall()
|
||
|
for row in records:
|
||
|
self.console_id = row[0]
|
||
|
cursor.close()
|
||
|
else:
|
||
|
for row in records:
|
||
|
self.console_id = row[0]
|
||
|
self.populateConsoleAttrs()
|
||
|
|
||
|
except sqlite3.Error as error:
|
||
|
print("Failed to fetch data from database: ", error)
|
||
|
|
||
|
finally:
|
||
|
if (sqliteConnection):
|
||
|
sqliteConnection.close()
|
||
|
print("The SQLite connection is closed")
|
||
|
|
||
|
elif name is None and console_id is not None:
|
||
|
self.console_id = console_id
|
||
|
db = dbConnection()
|
||
|
query = "SELECT * FROM Consoles WHERE console_id=?"
|
||
|
values = [console_id, ]
|
||
|
records = db.queryDB(query, values, returnsRecords=True)
|
||
|
if records.__sizeof__() > 0:
|
||
|
self.populateConsoleAttrs()
|
||
|
db.closeDB()
|
||
|
|
||
|
else:
|
||
|
print("No data given, initializing blank Console object")
|
||
|
|
||
|
def updateConsoleAttrs(self, console_id=None):
|
||
|
if console_id is None:
|
||
|
# print("Console ID is REQUIRED!")
|
||
|
return
|
||
|
else:
|
||
|
try:
|
||
|
sqliteConnection = sqlite3.connect('Romscraper.db')
|
||
|
cursor = sqliteConnection.cursor()
|
||
|
# print("Connected")
|
||
|
query = "SELECT * FROM Consoles WHERE console_id=?"
|
||
|
cursor.execute(query, [console_id, ])
|
||
|
records = cursor.fetchall()
|
||
|
if len(records) > 0:
|
||
|
# print("Console is not in table")
|
||
|
query = "UPDATE Consoles SET name=?, description=?, rating=?, thumbnail_uri=? WHERE console_id=?"
|
||
|
cursor.execute(query,
|
||
|
[self.name, self.description, self.rating, self.thumbnail_uri, self.console_id])
|
||
|
sqliteConnection.commit()
|
||
|
cursor.close()
|
||
|
else:
|
||
|
print("Console with this ID does not exist!")
|
||
|
|
||
|
except sqlite3.Error as error:
|
||
|
print("Failed to fetch data from database: ", error)
|
||
|
|
||
|
def populateConsoleAttrs(self):
|
||
|
db = dbConnection()
|
||
|
query = "SELECT * FROM Consoles WHERE console_id=?"
|
||
|
values = [self.console_id]
|
||
|
records = db.queryDB(query, values, returnsRecords=True)
|
||
|
for row in records:
|
||
|
self.name = row[1]
|
||
|
self.rating = row[4]
|
||
|
self.description = row[2]
|
||
|
return
|