61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
import sqlite3
|
|
|
|
|
|
class dbConnection:
|
|
dbname = 'Romscraper.db'
|
|
|
|
def __init__(self, dbname=None):
|
|
if dbname is not None:
|
|
self.dbname = dbname
|
|
|
|
try:
|
|
self.sqliteConnection = sqlite3.connect('Romscraper.db')
|
|
self.cursor = self.sqliteConnection.cursor()
|
|
except sqlite3.Error as error:
|
|
print("Failed to fetch data from database: ", error)
|
|
#finally:
|
|
#print("Database is initialized")
|
|
|
|
def queryDB(self, query=None, values=None, commit=None, returnsRecords=None):
|
|
|
|
if query is None:
|
|
print("This needs a query!")
|
|
return
|
|
else:
|
|
try:
|
|
if values is not None:
|
|
self.cursor.execute(query, values)
|
|
else:
|
|
self.cursor.execute(query)
|
|
|
|
except sqlite3.Error as error:
|
|
print("Failed to fetch data from database: ", error)
|
|
|
|
finally:
|
|
if commit is True:
|
|
self.sqliteConnection.commit()
|
|
if returnsRecords is True:
|
|
records = self.cursor.fetchall()
|
|
return records
|
|
|
|
|
|
def closeDB(self, commit=None):
|
|
if commit is None:
|
|
commit = False
|
|
|
|
if commit:
|
|
self.sqliteConnection.commit()
|
|
self.sqliteConnection.close()
|
|
return
|
|
|
|
def searchByName(self, table, name, fuzzymatch):
|
|
if fuzzymatch:
|
|
name = "%" + name + "%"
|
|
query = "SELECT * FROM " + table + " WHERE name LIKE ?"
|
|
else:
|
|
query = "SELECT * FROM " + table + " WHERE name = ?"
|
|
|
|
self.cursor.execute(query, [name])
|
|
records = self.cursor.fetchall()
|
|
return records
|