How to fix "Cannot operate on a closed cursor"?

364 views
Skip to first unread message

Brettschneider Falk

unread,
Jan 6, 2014, 4:08:18 AM1/6/14
to trac...@googlegroups.com

Hi,

Sometimes the following code let cursor.execute() run in the "Cannot operate on a closed cursor" issue (seen with Trac 1.0 and SQLite). I played around with several trials for a fix but nothing seems to help. Could you help me and correct the following code example so that I can see what the proper use of that db API is? Yes, I’ve read http://trac.edgewall.org/wiki/TracDev/DatabaseApi but it’s still not clear to me how I must fix the closed cursor error. Thanks in advance.

 

class SmpModel(Component):

 

    # DB Method

    def __get_cursor(self):

        if VERSION < '0.12':

            self.db = self.env.get_db_cnx()

        else:

            self.db = self.env.get_read_db()

        return self.db.cursor()

 

 

    def __start_transacction(self):

        if VERSION < '0.12':

            # deprecated in newer versions

            self.db.commit()

            self.db.close()

 

 

    def get_ticket_project(self, id):

        cursor = self.__get_cursor()

        query    = """SELECT

                        value

                      FROM

                        ticket_custom

                      WHERE

                        name = 'project' AND ticket = %s"""

        cursor.execute(query, [id])

        self.__start_transacction()

        return cursor.fetchone()

 

CU, F@lk

 

----

R&D Software

Baumer Optronic GmbH

www.baumer.com

 

Geschäftsführer: Dr. Albert Schmidt · Dr. Oliver Vietze
Sitz der Gesellschaft: Radeberg
Amtsgericht Dresden: HRB 15379
Ust.
ID: DE  189714583

 

 

falkb

unread,
Jan 6, 2014, 6:35:49 PM1/6/14
to trac...@googlegroups.com
I think I begin to understand now. It's time-consuming but it's getting clearer. There's a difference between read and write access to db now with Trac 1.0, and the scope of the db object can get lost....

CU, falkb


Am Montag, 6. Januar 2014 10:08:18 UTC+1 schrieb falkb:

Hi,

Sometimes the following code let cursor.execute() run in the "Cannot operate on a closed cursor" issue (seen with Trac 1.0 and SQLite). I played around with several trials for a fix but nothing seems to help. Could you help me and correct the following code example so that I can see what the proper use of that db API is? Yes, I’ve read http://trac.edgewall.org/wiki/TracDev/DatabaseApi but it’s still not clear to me ... snip

Reply all
Reply to author
Forward
0 new messages