MsRewards/modules/db.py

67 lines
2.4 KiB
Python

import sqlite3
# Create a new row, for the account [compte] whith [points] points
def add_row(account, points, mycursor, mydb):
sql = "INSERT INTO daily (compte, points, date) VALUES (?, ?, date())"
val = (account, points)
mycursor.execute(sql, val)
mydb.commit()
# printf(mycursor.rowcount, "record created.")
# update the ammount of points for the account [compte]
def update_row(account, points, mycursor, mydb):
sql = f"UPDATE daily SET points = {points} WHERE compte = '{account}' AND date = date() ;"
mycursor.execute(sql)
mydb.commit()
# update the value of last_pts for the table comptes
def update_last(account, points, mycursor, mydb):
sql1 = f"UPDATE comptes SET last_pts = {points} WHERE compte = '{account}';"
sql2 = f"select * from comptes where compte = '{account}'"
sql3 = f"INSERT INTO comptes (compte, last_pts,banned) VALUES ('{account}', {points}, 0)"
cmd = mycursor.execute(sql2)
if len(list(cmd)) == 0:
mycursor.execute(sql3)
else:
mycursor.execute(sql1)
mydb.commit()
# Return if there already is a line in the database for the account [account].
# if same_point is enabled, the line must also have the same number of points
# SQLITE
def get_row(account, points, mycursor, same_points=True):
if same_points:
mycursor.execute(f"SELECT * FROM daily WHERE points = {points} AND compte = '{account}' AND date = date() ;")
else:
mycursor.execute(f"SELECT * FROM daily WHERE compte = '{account}' AND date = date() ;")
myresult = mycursor.fetchall()
return (len(myresult) == 1)
def add_to_database(account, points):
if points is None:
pass
else:
mydb = sqlite3.connect("/app/MsRewards-Reborn/MsRewards.db")
mycursor = mydb.cursor()
if get_row(account, points, mycursor, True):
# check if the row exist with the same amount of points and do nothing if it does
pass
# check if the row exist, but without the same amount of points and update the point account then
elif get_row(account, points, mycursor, False):
update_row(account, points, mycursor, mydb)
else: # if the row don't exist, create it with the good amount of points
add_row(account, points, mycursor, mydb)
if int(points) > 10:
update_last(account, points, mycursor, mydb)
mycursor.close()
mydb.close()