def delete_from_db(self, money_id): cur = con.cursor() sql_data = { "money_id": money_id } sql_query = """DELETE FROM money WHERE id = %(money_id)s""" cur.execute(sql_query, sql_data)
def save_to_db(self): sql_data = { "player_id": self.player_id, "type": self.__class__.__name__, "name": self.name, "email": self.email, "password": self.password } insert_query = """INSERT INTO player (id, type, name, email, password, created) VALUES (%(player_id)s, %(type)s, %(name)s,%(email)s, %(password)s, now())""" update_query = """UPDATE player SET type=%(type)s, name=%(name)s, email=%(email)s, password=%(password)s, updated=now() WHERE id=%(player_id)s""" cur = con.cursor() cur.execute("SELECT id FROM player WHERE id=%s" % self.player_id) player_id_data = cur.fetchall() if player_id_data: cur.execute(update_query, sql_data) else: cur.execute(insert_query, sql_data) self.player_id = cur.lastrowid for x in self.sessions: x.save_to_db() for x in self.wallet.values(): x.save_to_db() for x in self.achievements: x.save_to_db() self.counter.save_to_db()
def delete_from_db_player(self, player_id): cur = con.cursor() sql_data = { "player_id": player_id } sql_query = """DELETE FROM session WHERE player_id=%(player_id)s""" cur.execute(sql_query, sql_data)
def delete_from_db(self, counter_id): cur = con.cursor() sql_data = { "counter_id": counter_id } sql_query = """DELETE FROM counter WHERE id = %(counter_id)s""" cur.execute(sql_query, sql_data)
def delete_from_db(self, session_id): cur = con.cursor() sql_data = { "session_id": session_id } sql_query = """DELETE FROM session WHERE id = %(session_id)s""" cur.execute(sql_query, sql_data)
def delete_from_db(self, achievement_id): cur = con.cursor() sql_data = { "achievement_id": achievement_id } sql_query = """DELETE FROM achievement WHERE id = %(achievement_id)s""" cur.execute(sql_query, sql_data)
def delete_from_db(self, player_id): Money().delete_from_db_player(player_id) Session().delete_from_db_player(player_id) Counter().delete_from_db_player(player_id) Achievement().delete_from_db_player(player_id) cur = con.cursor() delete_player_query = """DELETE FROM player WHERE id=%(player_id)s""" sql_data = { "player_id": player_id } cur.execute(delete_player_query, sql_data)
def load_from_db_player(self, player_id): cur = con.cursor() sql_data = { "player_id": player_id } sql_query = """SELECT id FROM counter WHERE player_id=%(player_id)s""" cur.execute(sql_query, sql_data) counter_id = cur.fetchone() data = Counter() data.load_from_db(counter_id) return data
def load_from_db_player(self, player_id): cur = con.cursor() sql_data = { "player_id": player_id } sql_query = """SELECT id FROM session WHERE player_id=%(player_id)s""" cur.execute(sql_query, sql_data) session_id = cur.fetchall() result = [] for x in session_id: data = Session() data.load_from_db(x) result.append(data) return result
def load_from_db_player(self, player_id): cur = con.cursor() sql_data = { "player_id": player_id } sql_query = """SELECT id FROM achievement WHERE player_id=%(player_id)s""" cur.execute(sql_query, sql_data) achievement_id = cur.fetchall() result = [] for x in achievement_id: data = Achievement() data.load_from_db(x) result.append(data) return result
def load_from_db_player(self, player_id): cur = con.cursor() sql_data = { "player_id": player_id } sql_query = """SELECT id FROM money WHERE player_id=%(player_id)s""" cur.execute(sql_query, sql_data) money_id = cur.fetchall() result = {} for x in money_id: data = Money() data.load_from_db(x) result[data.code] = data return result
def load_from_db(self, money_id): cur = con.cursor() sql_data = { "money_id": money_id } sql_query = """SELECT id, player_id, code, amount, created, updated FROM money WHERE id=%(money_id)s""" cur.execute(sql_query, sql_data) row = cur.fetchone() self.money_id = row[0] self.player_id = row[1] self.code = row[2] self.amount = row[3] self.created = row[4] self.updated = row[5]
def load_from_db(self, achievement_id): cur = con.cursor() sql_data = { "achievement_id": achievement_id } sql_query = """SELECT id, player_id, name, description, created, updated FROM achievement WHERE id=%(achievement_id)s""" cur.execute(sql_query, sql_data) row = cur.fetchone() self.achievement_id = row[0] self.player_id = row[1] self.name = row[2] self.description = row[3] self.created = row[4] self.updated = row[5]
def load_from_db(self, counter_id): cur = con.cursor() sql_data = { "counter_id": counter_id } sql_query = """SELECT id, player_id, games, win, lose, created, updated FROM counter WHERE id=%(counter_id)s""" cur.execute(sql_query, sql_data) row = cur.fetchone() self.counter_id = row[0] self.player_id = row[1] self.games = row[2] self.win = row[3] self.lose = row[4] self.created = row[5] self.updated = row[6]
def load_from_db(self, session_id): cur = con.cursor() sql_data = { "session_id": session_id } sql_query = """SELECT id, player_id, start_time, finish_time, created, updated FROM session WHERE id=%(session_id)s""" cur.execute(sql_query, sql_data) row = cur.fetchone() self.session_id = row[0] self.player_id = row[1] self.start_time = row[2] self.finish_time = row[3] self.created = row[4] self.updated = row[5]
def save_to_db(self): sql_data = { "player_id": self.player_id, "name": self.name, "description": self.description } sql_data_search = { "achievement_id": self.achievement_id } sql_query_search = """SELECT id FROM achievement WHERE id=%(achievement_id)s""" insert_query = """INSERT INTO achievement (player_id, name, description, created) VALUES (%(player_id)s, %(name)s, %(description)s, now())""" update_query = """UPDATE achievement SET name=%(name)s, description=%(description)s, updated=now() WHERE id=%(achievement_id)s""" cur = con.cursor() cur.execute(sql_query_search, sql_data_search) session_id_data = cur.fetchall() if session_id_data: sql_data["achievement_id"] = self.achievement_id cur.execute(update_query, sql_data) else: cur.execute(insert_query, sql_data) self.achievement_id = cur.lastrowid
def save_to_db(self): sql_data = { "player_id": self.player_id, "code": self.code, "amount": self.amount } sql_data_search = { "money_id": self.money_id } sql_query_search = """SELECT id FROM money WHERE id=%(money_id)s""" insert_query = """INSERT INTO money (player_id, code, amount, created) VALUES (%(player_id)s, %(code)s, %(amount)s, now())""" update_query = """UPDATE money SET code=%(code)s, amount=%(amount)s, updated=now() WHERE id=%(money_id)s""" cur = con.cursor() cur.execute(sql_query_search, sql_data_search) session_id_data = cur.fetchall() if session_id_data: sql_data["money_id"] = self.money_id cur.execute(update_query, sql_data) else: cur.execute(insert_query, sql_data) self.money_id = cur.lastrowid
def save_to_db(self): sql_data = { "player_id": self.player_id, "games": self.games, "win": self.win, "lose": self.lose } sql_data_search = { "counter_id": self.counter_id } sql_query_search = """SELECT id FROM counter WHERE id=%(counter_id)s""" insert_query = """INSERT INTO counter (player_id, games, win, lose, created) VALUES (%(player_id)s, %(games)s, %(win)s, %(lose)s, now())""" update_query = """UPDATE counter SET games=%(games)s, win=%(win)s, lose=%(lose)s, updated=now() WHERE player_id=%(player_id)s""" cur = con.cursor() cur.execute(sql_query_search, sql_data_search) session_id_data = cur.fetchall() if session_id_data: sql_data["counter_id"] = self.counter_id cur.execute(update_query, sql_data) else: cur.execute(insert_query, sql_data) self.counter_id = cur.lastrowid
def save_to_db(self): sql_data = { "player_id": self.player_id, "start_time": self.start_time, "finish_time": self.finish_time } sql_data_search = { "session_id": self.session_id } sql_query_search = """SELECT id FROM session WHERE id=%(session_id)s""" update_query = """UPDATE session SET start_time=%(start_time)s, finish_time=%(finish_time)s, updated=now() WHERE id=%(session_id)s""" insert_query = """INSERT INTO session (player_id, start_time, finish_time, created) VALUES (%(player_id)s, %(start_time)s, %(finish_time)s, now())""" cur = con.cursor() cur.execute(sql_query_search, sql_data_search) session_id_data = cur.fetchall() if session_id_data: sql_data["session_id"] = self.session_id cur.execute(update_query, sql_data) else: cur.execute(insert_query, sql_data) self.session_id = cur.lastrowid
def load_from_db(self, email): cur = con.cursor() sql_data_email = { "email": email } sql_query = """SELECT id, type, name, email, password, created, updated FROM player WHERE email=%(email)s""" cur.execute(sql_query, sql_data_email) row = cur.fetchone() self.player_id = row[0] self.name = row[2] self.email = row[3] self.password = row[4] self.created = row[5] self.updated = row[6] if row[1] == 'Player': self.__class__ = Player elif row[1] == 'Admin': self.__class__ = Admin else: self.__class__ = Moder self.sessions = Session().load_from_db_player(self.player_id) self.wallet = Money().load_from_db_player(self.player_id) self.achievements = Achievement().load_from_db_player(self.player_id) self.counter = Counter().load_from_db_player(self.player_id)
from db import con cur = con.cursor() cur.execute("SELECT * FROM customer") data = cur.fetchall() print(data)