From ca3e6868dcfc6452f681ae1de65ae2eb9e05b0c0 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 4 Oct 2012 18:54:49 +0800 Subject: [PATCH] Enhanced database error handling (escaping, connexion) --- lib/db.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/db.py b/lib/db.py index 8f62a5f..3a5eed3 100644 --- a/lib/db.py +++ b/lib/db.py @@ -34,7 +34,10 @@ class DB(object): password = str(options['passwd']), dbname = '' ) - self.cur = self.conn.cursor() + try: + self.cur = self.conn.cursor() + except: + raise Exception('Connexion failed! Make sure the database \'%s\' exists.' % str(options['user'])) else: raise Exception('DB engine %s not supported' % engine) @@ -58,9 +61,9 @@ class DB(object): def createdb(self, db): if self.engine == 'mysqli': - self.cur.execute('CREATE DATABASE %s DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci' % db) + self.cur.execute('CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci' % db) elif self.engine == 'pgsql': - self.cur.execute('CREATE DATABASE %s WITH ENCODING \'UNICODE\'' % db) + self.cur.execute('CREATE DATABASE "%s" WITH ENCODING \'UNICODE\'' % db) def dbexists(self, db): @@ -77,7 +80,10 @@ class DB(object): def dropdb(self, db): - self.cur.execute('DROP DATABASE %s' % db) + if self.engine == 'mysqli': + self.cur.execute('DROP DATABASE `%s`' % db) + elif self.engine == 'pgsql': + self.cur.execute('DROP DATABASE "%s"' % db) def dump(self, fd, prefix = ''): """Dump a database to the file descriptor passed""" -- 2.11.0