def create_db_hiscore(): with MySQL.UseDatabase(config) as cursor: cursor.execute("""drop table if exists hiscore;""") cursor.execute( """create table hiscore(hiscore_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, score_amt INT NOT NULL, player_sn varchar(16) NOT NULL);""" ) print("Table hiscore created!")
def check_guess(guess): with MySQL.UseDatabase(config) as cursor: cursor.execute("""SELECT * from dict where word_sn = %s;""", (guess, )) if len(cursor.fetchall()) == 0: return False else: return True
def create_db_dict(): with MySQL.UseDatabase(config) as cursor: cursor.execute("""drop table if exists dict;""") cursor.execute( """create table dict(dict_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, word_sn VARCHAR(24));""" ) print("Table dict created!")
def hiscore_to_db(): if check_score_list(): with MySQL.UseDatabase(config) as cursor: for item in scorelist: cursor.execute( """INSERT INTO hiscore(player_sn, score_amt) VALUES(%s, %s);""", (item[0], item[1])) print("Scores inserted into database!")
def dict_to_db(): if check_dict_lists(): with MySQL.UseDatabase(config) as cursor: #All short words of guesslist for guess in guesslist: if len(guess) < 7: cursor.execute("""INSERT INTO dict(word_sn) VALUES(%s);""", (guess, )) #Any other words in sourcelist (filtered for unwinnable) for source in sourcelist: cursor.execute("""INSERT INTO dict(word_sn) VALUES(%s);""", (source, )) print("Dictionaries inserted into database!")
def get_top_ten(): with MySQL.UseDatabase(config) as cursor: cursor.execute( """SELECT player_sn, score_amt FROM hiscore ORDER BY score_amt DESC, hiscore_id ASC LIMIT 10;""" ) return cursor.fetchall()
def insert_hiscore(name, score): with MySQL.UseDatabase(config) as cursor: cursor.execute( """INSERT INTO hiscore(player_sn, score_amt) VALUES(%s, %s);""", (str(name), int(score)))
def get_random_source(): with MySQL.UseDatabase(config) as cursor: cursor.execute( """SELECT word_sn FROM dict WHERE CHAR_LENGTH(word_sn) >= 7 ORDER BY RAND() LIMIT 1;""" ) return cursor.fetchall()[0][0]