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.
77 lines
2.9 KiB
77 lines
2.9 KiB
import sqlite3 |
|
from dbHandler import dbConnection |
|
|
|
|
|
class Website: |
|
website_id = 0 |
|
name = "" |
|
url = "" |
|
description = "" |
|
thumbnail_uri = "" |
|
rating = 0 |
|
|
|
def __init__(self, website_id=None, name=None): |
|
if website_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 Websites WHERE name=?" |
|
cursor.execute(query, [name, ]) |
|
records = cursor.fetchall() |
|
if len(records) < 1: |
|
print("Website is not in table") |
|
query = "INSERT INTO Websites (name) VALUES (?)" |
|
cursor.execute(query, [name, ]) |
|
sqliteConnection.commit() |
|
cursor.close() |
|
else: |
|
for row in records: |
|
self.website_id = row[0] |
|
self.url = row[2] |
|
|
|
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 website_id is not None: |
|
db = dbConnection() |
|
query = "SELECT * FROM Websites WHERE website_id=?" |
|
values = [website_id, ] |
|
records = db.queryDB(query, values, returnsRecords=True) |
|
for row in records: |
|
self.website_id = row[0] |
|
self.url = row[2] |
|
db.closeDB() |
|
|
|
else: |
|
print("No data given, initializing blank Website object") |
|
|
|
def updateWebsiteAttrs(self, website_id=None): |
|
if website_id is None: |
|
print("Website ID is REQUIRED!") |
|
return |
|
else: |
|
try: |
|
sqliteConnection = sqlite3.connect('Romscraper.db') |
|
cursor = sqliteConnection.cursor() |
|
print("Connected") |
|
query = "SELECT * FROM Websites WHERE website_id=?" |
|
cursor.execute(query, [website_id, ]) |
|
records = cursor.fetchall() |
|
if len(records) > 0: |
|
print("Website is not in table") |
|
query = "UPDATE Websites SET name=?, url=?, description=?, rating=?, thumbnail_uri=? WHERE website_id=? " |
|
cursor.execute(query, [self.name, self.url, self.description, self.rating, self.thumbnail_uri, self.website_id]) |
|
sqliteConnection.commit() |
|
cursor.close() |
|
else: |
|
print("Website with this ID does not exist!") |
|
|
|
except sqlite3.Error as error: |
|
print("Failed to fetch data from database: ", error)
|
|
|