[Tutor] The case of the missing close

Kent Johnson kent37 at tds.net
Wed Dec 1 11:53:32 CET 2004


Here is a sketch to get you started. I'm not sure how cursors work so I may be off the mark! You may 
want to keep the cursor as an attribute of db so you can reuse it?

class db:
   def __init__(self, db, uname, passwd)
     self.connection = cx_Oracle.connect(dsn=db,user=uname,password=passwd)

   def execute(self, sql):
     cursor = connection.cursor()
     cursor.execute(sql)
     return cursor

   def close(self):
     self.connection.close()
     self.connection = None # Prevent reusing a closed connection


Your client code would look like this:

import db_utils

dbobj = db_utils.db('test_d','FOO','foo')

cursobj = dbobj.execute('SELECT userid, name, role, desk_phone, pager FROM
contacts')

results=cursobj.fetchall()

for row in results:
     print row

dbobj.close()


Kent

Robert, Andrew wrote:
> Hi Kent,
> 
> I tried returning the connection but that didn't work either.
> 
> In fact, returning the connection fails to create the cursor object in the calling program and the whole program dies a miserable death.
> 
> I am intrigued about the idea of a class though.
> 
> I've never really had the need to use them before now but it seems like now is a good time to start.
> 
> I'll let you know how things work out.
> 
> 
> 
> -----Original Message-----
> From: tutor-bounces at python.org on behalf of Kent Johnson
> Sent: Tue 11/30/2004 10:21 PM
> Cc: tutor at python.org
> Subject: Re: [Tutor] The case of the missing close
>  
> I don't know cx_Oracle, but my guess is you want to close the actual connection object, not the 
> cursor. Your dbopen() function doesn't expose the connection.
> 
> One option would be to return the connection rather than the cursor. Then clients could close the 
> connection when done. Another option is to create a class that holds both the connection and the 
> cursor, then you can have close() on the class actually close the connection.
> 
> Kent
> 
> 
> "MFS Relay Service" made the following
>  annotations on 12/01/2004 05:15:44 AM
> ------------------------------------------------------------------------------
> This email communication and any attachments may contain proprietary, confidential, or privileged information.  If you are not the intended recipient, you are hereby notified that you have received this email in error and that any review, disclosure, dissemination, distribution or copying of it or its contents is prohibited.  The sender does not waive confidentiality or any privilege by mistransmission.  If you have received this email in error, please notify the sender immediately, delete this email, and destroy all copies and any attachments.
> ==============================================================================
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list