Ist es "normal" bzw sinnvoll dass man mit so vielen try...except blöcken arbeitet? Sieht irgendwie komisch aus, und manches hiervon ist evtl totaler Schwachsinn, aber naja. Wen soll man fragen, wenn man alleine zuhause lernt und tüftelt
Mir ist klar dass ich das Forum evtl unpassend gewählt habe, immerhin frage ich ja nach etwas recht allgemeinem (Einsatz von try...except), aber es ist nun mal doch datenbankbezogen
Mir geht es primär um die query() methode:
Code: Alles auswählen
class SqliteDB:
"""Executes database queries for the User class"""
def __init__(self, db_file):
self._db_file = db_file
try:
self._conn = sqlite3.connect(self._db_file)
except Exception, e:
print "Error: Sqlite connection not available.\n\t%s" % e
@property
def db_file(self):
"""Gets the database file name or path."""
return self._db_file
@property
def conn(self):
"""Gets the connection object."""
return self._conn
def query(self, sql=''):
# get the db cursor
try:
cursor = self.conn.cursor()
except Exception, e:
print "Error: Cursor not available.\n\t%s" % e
# execute query
try:
cursor.execute(sql)
except Exception, e:
print "Error: SQL query failed.\n\t%s" % e
# commit the query to the database
try:
conn.commit()
except Exception, e:
print "Error: SQL query not committed.\n\t%s" % e
# fetch the results
try:
results = cursor.fetchall()
except Exception, e:
print "Error: Could not fetch the results.\n\t%s" % e
# close the cursor
cursor.close()
# return results
return results
Mich interessiert auch, ob der Code stilmäßig vernünftig aussieht, bin natürlich am lernen und offen für Verbesserungen.