def insert_state(id, state): try: with connection.cursor() as cursor: query = "insert into db_state value('{}', '{}')".format(id, state) cursor.execute(query) connection.commit() except: raise Exception("db_state error")
def restart_table(): try: with connection.cursor() as cursor: query = "UPDATE db_defult_words set in_use = 0;" cursor.execute(query) connection.commit() except: raise Exception("DBError - db default words")
def restart_index_col(): try: with connection.cursor() as cursor: query = "UPDATE db_image set _index = 0;" cursor.execute(query) connection.commit() except: raise Exception("DBError - db image")
def update_state(id, state): try: with connection.cursor() as cursor: query = "update db_state set _state = '{}' where id = {}".format( state, id) cursor.execute(query) connection.commit() except: raise Exception("db_state error")
def get_number_of_words(): try: with connection.cursor() as cursor: query_count = "SELECT COUNT(word) as count FROM db_defult_words;" cursor.execute(query_count) count = cursor.fetchall() return count[0]["count"] except: raise Exception("DBError - db default words")
def update_in_use(result): try: with connection.cursor() as cursor: query = "UPDATE db_image set _index = {} where word = %s;".format( 1) val = (result) cursor.execute(query, val) connection.commit() except: raise Exception("DBError - db image")
def get_state(id): try: with connection.cursor() as cursor: query = "select _state from db_state where id = {}".format(id) cursor.execute(query) result = cursor.fetchall() if result: return result[0]["_state"] return None except: raise Exception("db_state error")
def update_in_use(words): try: c = '"' with connection.cursor() as cursor: for word in words: query = "UPDATE db_defult_words set in_use = 1 where word = " + c + word[ "word"] + c + ";" cursor.execute(query) connection.commit() except: raise Exception("DBError - db default words")
def get_word_image(): try: with connection.cursor() as cursor: query = "SELECT word from db_image where _index = {};".format(0) cursor.execute(query) result = cursor.fetchall() if result == (): restart_index_col() query = "SELECT word FROM db_image where _index = {};".format(0) cursor.execute(query) result = cursor.fetchall()[0]["word"] update_in_use(result) return result except: raise Exception("db_image error")
def find_words_unused(num): try: number_of_words = get_number_of_words() num = min(num, number_of_words) with connection.cursor() as cursor: query_count = "SELECT COUNT(word) as count FROM db_defult_words where in_use=0;" cursor.execute(query_count) count = cursor.fetchall() if (count[0]["count"] < num): restart_table() query = "SELECT word FROM db_defult_words where in_use = 0 limit " + str( num) + ";" cursor.execute(query) result = cursor.fetchall() update_in_use(result) return result except Exception as e: return e