add try-except blocks and correct spelling

This commit is contained in:
sqlcoder 2009-05-21 18:02:42 +01:00
parent fcc6cf5d4f
commit 0651e89e2e

View File

@ -59,7 +59,10 @@ class fpdb_db:
self.database=database self.database=database
if backend==self.MYSQL_INNODB: if backend==self.MYSQL_INNODB:
import MySQLdb import MySQLdb
self.db=MySQLdb.connect(host = host, user = user, passwd = password, db = database, use_unicode=True) try:
self.db = MySQLdb.connect(host = host, user = user, passwd = password, db = database, use_unicode=True)
except:
raise fpdb_simple.FpdbError("MySQL connection failed")
elif backend==self.PGSQL: elif backend==self.PGSQL:
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
@ -68,15 +71,21 @@ class fpdb_db:
# host, user and password are required # host, user and password are required
print "host=%s user=%s pass=%s." % (host, user, password) print "host=%s user=%s pass=%s." % (host, user, password)
if self.host and self.user and self.password: if self.host and self.user and self.password:
self.db = psycopg2.connect(host = host, try:
user = user, self.db = psycopg2.connect(host = host,
password = password, user = user,
database = database) password = password,
database = database)
except:
raise fpdb_simple.FpdbError("PostgreSQL connection failed")
# For local domain-socket connections, only DB name is # For local domain-socket connections, only DB name is
# needed, and everything else is in fact undefined and/or # needed, and everything else is in fact undefined and/or
# flat out wrong # flat out wrong
else: else:
self.db = psycopg2.connect(database = database) try:
self.db = psycopg2.connect(database = database)
except:
raise fpdb_simple.FpdbError("PostgreSQL connection failed")
else: else:
raise fpdb_simple.FpdbError("unrecognised database backend:"+backend) raise fpdb_simple.FpdbError("unrecognised database backend:"+backend)
self.cursor=self.db.cursor() self.cursor=self.db.cursor()
@ -138,7 +147,7 @@ class fpdb_db:
if(self.get_backend_name() == 'MySQL InnoDB'): if(self.get_backend_name() == 'MySQL InnoDB'):
#Databases with FOREIGN KEY support need this switched of before you can drop tables #Databases with FOREIGN KEY support need this switched of before you can drop tables
self.drop_referencial_integrity() self.drop_referential_integrity()
# Query the DB to see what tables exist # Query the DB to see what tables exist
self.cursor.execute(self.sql.query['list_tables']) self.cursor.execute(self.sql.query['list_tables'])
@ -157,7 +166,7 @@ class fpdb_db:
self.db.commit() self.db.commit()
#end def drop_tables #end def drop_tables
def drop_referencial_integrity(self): def drop_referential_integrity(self):
"""Update all tables to remove foreign keys""" """Update all tables to remove foreign keys"""
self.cursor.execute(self.sql.query['list_tables']) self.cursor.execute(self.sql.query['list_tables'])
@ -175,7 +184,7 @@ class fpdb_db:
key = "`" + inner[j][0] + "_" + m.group() + "`" key = "`" + inner[j][0] + "_" + m.group() + "`"
self.cursor.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key) self.cursor.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key)
self.db.commit() self.db.commit()
#end drop_referencial_inegrity #end drop_referential_inegrity
def get_backend_name(self): def get_backend_name(self):
"""Returns the name of the currently used backend""" """Returns the name of the currently used backend"""