[DB-SIG] Python db programming conventions
Chris Clark
Chris.Clark at ingres.com
Mon Nov 22 18:19:09 CET 2010
Andy Dustman wrote:
> On Sun, Nov 21, 2010 at 3:02 PM, John Q. Public <sqlite3.user at gmail.com> wrote:
>
>> Is this how my createdb() method should look like?
>> How would you write this method so it is both readable and correct?
>> Thank you for your time and patience.
>>
>> def createdb(self):
>> try:
>> con = sqlite3.connect(db)
>> cur = con.cursor()
>>
>> cur.execute('''
>> CREATE TABLE t1
>> (
>> kid INTEGER PRIMARY KEY,
>> c1 TEXT,
>> c2 TEXT
>> )
>> ''')
>>
>> cur.execute('''
>> CREATE TABLE t2
>> (
>> kid INTEGER PRIMARY KEY,
>> c1 TEXT,
>> c2 TEXT
>> )
>> ''')
>>
>> cur.execute('''
>> CREATE TABLE t3
>> (
>> kid INTEGER PRIMARY KEY,
>> c1 TEXT,
>> c2 TEXT
>> )
>> ''')
>>
>> con.commit()
>> except:
>> a = "ERROR: createdb did not commit. \n"
>> b = "tried this sql: \n"
>> raise IOError, "%s%s%s" % ( a, b, sql )
>> finally:
>> cur.close()
>> con.close()
>>
>
> You never actually set sql anywhere, so you'll always get a NameError
> instead of IOError. It would probably be better to not catch the
> exception at all in this function.
>
If you are using Nose you can use this plugin
http://exogen.github.com/nose-achievements/ and gain achievements, the
code above should net you the; "Silence! I keel you!" achievement :-)
All the achievements are visible at
https://docs.google.com/View?id=dfsf8s3r_45388t5rdd
unlocked the "Silence! I keel you!" achievement by checking in code
with only bare except: clauses.
Chris
More information about the DB-SIG
mailing list