Ledger now inherits from Database
This commit is contained in:
41
Ledger.py
41
Ledger.py
@@ -1,27 +1,43 @@
|
||||
import sqlite3
|
||||
|
||||
class Ledger():
|
||||
def __init__(self) -> None:
|
||||
self.db = sqlite3.connect("ledger.db")
|
||||
class Database():
|
||||
def __init__(self, name, tableCommand) -> None:
|
||||
self.db = sqlite3.connect(name)
|
||||
self.data = self.db.cursor()
|
||||
self.data.execute(""" CREATE TABLE IF NOT EXISTS ledger (
|
||||
self.data.execute(tableCommand)
|
||||
|
||||
def read(self, primaryKey, selectQuery):
|
||||
self.data.execute(selectQuery, (primaryKey,))
|
||||
data = self.data.fetchone()
|
||||
return data
|
||||
|
||||
def write(self, ID, insertQuery):
|
||||
self.data.execute(insertQuery, (ID,))
|
||||
self.db.commit()
|
||||
|
||||
def update(self, data, newData, updateQuery):
|
||||
data = [data[i] + newData[i] for i in range(len(data))]
|
||||
self.data.execute(updateQuery, data)
|
||||
|
||||
class Ledger(Database):
|
||||
def __init__(self) -> None:
|
||||
tableCommand = """ CREATE TABLE IF NOT EXISTS ledger (
|
||||
ID integer PRIMARY KEY,
|
||||
USERID integer,
|
||||
MONEY integer DEFAULT 100,
|
||||
WINS integer DEFAULT 0,
|
||||
LOSSES integer DEFAULT 0
|
||||
); """)
|
||||
); """
|
||||
super().__init__("ledger.db", tableCommand)
|
||||
|
||||
def read(self, ID):
|
||||
self.data.execute("""SELECT USERID, MONEY, WINS, LOSSES FROM ledger WHERE USERID = ?""", (ID,))
|
||||
data = self.data.fetchone()
|
||||
selectQuery = """SELECT USERID, MONEY, WINS, LOSSES FROM ledger WHERE USERID = ?"""
|
||||
data = super().read(ID, selectQuery)
|
||||
return data
|
||||
|
||||
def write(self, ID):
|
||||
query = """ INSERT INTO ledger(USERID)
|
||||
insertQuery = """ INSERT INTO ledger(USERID)
|
||||
VALUES(?) """
|
||||
self.data.execute(query, (ID,))
|
||||
self.db.commit()
|
||||
super().read(ID, insertQuery)
|
||||
|
||||
def update(self, data, newData):
|
||||
query = """ UPDATE ledger
|
||||
@@ -29,8 +45,7 @@ class Ledger():
|
||||
WINS = ?,
|
||||
LOSSES = ?,
|
||||
WHERE USERID = ?"""
|
||||
data = [data[i] + newData[i] for i in range(len(data))]
|
||||
self.data.execute(query, data)
|
||||
super().update(data, newData, query)
|
||||
|
||||
def main():
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user