Good afternoon Chaps,

 

I'm looking for your advice on the best way to handle exceptions on a query
and commit. Within my application I have a universal database connection
which gets injected into objects which require database access, I then use
this connection to create cursors, run queries and commit the changes to the
database. Does that sounds like a good approach?

 

Take a look at this query below:

 

        # Create the Cursor for the database.

        cursor = self.datasource.cursor()

        

        # Run the insert query to create the entry.

        cursor.execute("""

                        INSERT INTO message (

                                            message_id,

                                            name,

                                            checksum,

                                            created, 

                                            modified

                                            ) 

                        VALUES (

                                %s,

                                %s,

                                %s,

                                now(), 

                                now()

                                )""", (self.message_id, self.name,
self.checksum))

 

        # Commit the change to the database.

        self.datasource.commit()

        

        # Close the cursor for the database.

        cursor.close()

 

Now to add some exception handling to that query, do I simply wrap the whole
code block up in a try/except, and handle any exceptions? or should I be
doing a nested try/except which only runs the commit method is the execute
runs without exception, and likewise only closes the cursor if it opens
successfully?

 

I'd really appreciate your advice on this, I'm relatively new to the world
of db implementation using the dbapi and want to make this as water tight as
possible.

 

Many thanks,

 

Heston James.

_______________________________________________
DB-SIG maillist  -  DB-SIG@python.org
http://mail.python.org/mailman/listinfo/db-sig

Reply via email to