From 391a75ab186aba45efb916b7742cfd27bb2a531f Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Sun, 23 Oct 2016 12:39:15 +0100 Subject: [PATCH] Abstract table entry creation. --- db.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index 7c07d7a..3572a47 100644 --- a/db.py +++ b/db.py @@ -128,6 +128,9 @@ class DBConnection(object): self.close_db() def create_announcement(self, client, **args): + return self.insert_into_table(client, 'announcements', **args) + + def insert_into_table(self, client, table, **args): id = self.uuid() values = [] @@ -135,13 +138,13 @@ class DBConnection(object): for k, v in args.items(): values.append(k) params.append(v) - sql = 'insert into announcements (id, bot_id, {}) values (?, ?, {})'.format(', '.join(values), ', '.join(['?'] * len(values))) + sql = 'insert into {} (id, bot_id, {}) values (?, ?, {})'.format(table, ', '.join(values), ', '.join(['?'] * len(values))) cursor = self.query(sql, params) if cursor.rowcount: self.dbh.commit() ret = id else: - log.error('Failed to create announcement {}: {}'.format(id, args)) + log.error('Failed to create entry {} in table {}: {}'.format(id, table, args)) self.dbh.rollback() ret = None self.close_db() -- 2.7.4