From 8386e45343772ac62c57bab99a4330eeee34845d Mon Sep 17 00:00:00 2001 From: jesopo Date: Sat, 18 Aug 2018 21:53:50 +0100 Subject: [PATCH] check if a table exists before making it instead of catching the sqlite exception --- Database.py | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/Database.py b/Database.py index a0f342fa..0691ce25 100644 --- a/Database.py +++ b/Database.py @@ -265,65 +265,59 @@ class Database(object): def execute(self, query, params=[]): return self._execute_fetch(query, lambda cursor: None, params) + def has_table(self, table_name): + result = self.execute_fetchone("""SELECT COUNT(*) FROM + sqlite_master WHERE type='table' AND name=?""", + [table_name]) + return result[0] == 1 + def make_servers_table(self): - try: + if not self.has_table("servers"): self.execute("""CREATE TABLE servers (server_id INTEGER PRIMARY KEY, hostname TEXT, port INTEGER, password TEXT, ipv4 BOOLEAN, tls BOOLEAN, nickname TEXT, username TEXT, realname TEXT)""") - except sqlite3.Error as e: - pass def make_bot_settings_table(self): - try: + if not self.has_table("bot_settings"): self.execute("""CREATE TABLE bot_settings (setting TEXT PRIMARY KEY, value TEXT)""") - except sqlite3.Error as e: - return - self.execute("""CREATE INDEX bot_settings_index ON - bot_settings (setting)""") + self.execute("""CREATE INDEX bot_settings_index + ON bot_settings (setting)""") def make_server_settings_table(self): - try: + if not self.has_table("server_settings"): self.execute("""CREATE TABLE server_settings (server_id INTEGER, setting TEXT, value TEXT, FOREIGN KEY(server_id) REFERENCES servers(server_id) ON DELETE CASCADE, PRIMARY KEY (server_id, setting))""") - except sqlite3.Error as e: - return - self.execute("""CREATE INDEX server_settings_index ON - server_settings (server_id, setting)""") + self.execute("""CREATE INDEX server_settings_index + ON server_settings (server_id, setting)""") def make_channel_settings_table(self): - try: + if not self.has_table("channel_settings"): self.execute("""CREATE TABLE channel_settings (server_id INTEGER, channel TEXT, setting TEXT, value TEXT, FOREIGN KEY (server_id) REFERENCES servers(server_id) ON DELETE CASCADE, PRIMARY KEY (server_id, channel, setting))""") - except sqlite3.Error as e: - return - self.execute("""CREATE INDEX channel_settings_index ON - channel_settings (server_id, channel, setting)""") + self.execute("""CREATE INDEX channel_settings_index + ON channel_settings (server_id, channel, setting)""") def make_user_settings_table(self): - try: + if not self.has_table("user_settings"): self.execute("""CREATE TABLE user_settings (server_id INTEGER, nickname TEXT, setting TEXT, value TEXT, FOREIGN KEY (server_id) REFERENCES servers(server_id) ON DELETE CASCADE, PRIMARY KEY (server_id, nickname, setting))""") - except sqlite3.Error as e: - return - self.execute("""CREATE INDEX user_settings_index ON - user_settings (server_id, nickname, setting)""") + self.execute("""CREATE INDEX user_settings_index ON + user_settings (server_id, nickname, setting)""") def make_user_channel_settings_table(self): - try: + if not self.has_table("user_channel_settings"): self.execute("""CREATE TABLE user_channel_settings (server_id INTEGER, channel TEXT, nickname TEXT, setting TEXT, value TEXT, FOREIGN KEY (server_id) REFERENCES servers(server_id) ON DELETE CASCADE, PRIMARY KEY (server_id, nickname, channel, setting))""") - except sqlite3.Error as e: - return - self.execute("""CREATE INDEX user_channel_settings_index - ON user_channel_settings (server_id, nickname, channel, - setting)""") + self.execute("""CREATE INDEX user_channel_settings_index + ON user_channel_settings (server_id, nickname, channel, + setting)""")