From 65eec1049df0ee1c8ecb9d6fcb38096b3743f44f Mon Sep 17 00:00:00 2001 From: memgonzales Date: Fri, 1 Sep 2023 15:04:57 +0800 Subject: [PATCH] Use constants in queries --- app.py | 4 +--- callbacks/constants.py | 1 + callbacks/file_util.py | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index aff89c79..93564ec3 100644 --- a/app.py +++ b/app.py @@ -268,9 +268,7 @@ connection = sqlite3.connect(const.FILE_STATUS_DB) cursor = connection.cursor() - query = '''CREATE TABLE file_table ( - name TEXT, - UNIQUE(name));''' + query = f'CREATE TABLE IF NOT EXISTS {const.FILE_STATUS_TABLE} (name TEXT, UNIQUE(name));' cursor.execute(query) connection.commit() diff --git a/callbacks/constants.py b/callbacks/constants.py index 28e0d993..a3fd0212 100644 --- a/callbacks/constants.py +++ b/callbacks/constants.py @@ -57,6 +57,7 @@ class Constants(object): # ========= FILE_STATUS_DB = f'{TEMP}/file_status.db' + FILE_STATUS_TABLE = 'file_status' def __init__(self): pass diff --git a/callbacks/file_util.py b/callbacks/file_util.py index b89fd947..37e8b82c 100644 --- a/callbacks/file_util.py +++ b/callbacks/file_util.py @@ -78,7 +78,7 @@ def shorten_name(name): connection = sqlite3.connect(const.FILE_STATUS_DB) cursor = connection.cursor() - query = f'INSERT OR IGNORE INTO file_table(name) VALUES("{name}")' + query = f'INSERT OR IGNORE INTO {const.FILE_STATUS_TABLE}(name) VALUES("{name}")' cursor.execute(query) connection.commit() @@ -92,7 +92,7 @@ def shorten_name(name): connection = sqlite3.connect(const.FILE_STATUS_DB) cursor = connection.cursor() - query = f'SELECT rowid FROM file_table WHERE name = "{name}"' + query = f'SELECT rowid FROM {const.FILE_STATUS_TABLE} WHERE name = "{name}"' cursor.execute(query) row_id = cursor.fetchall()[0][0]