views:

30

answers:

1

I'm trying to come up with SQLiteDB object, and following is the open/close code for it. Does this work without problem? Am I missing something important?

For close(), I use con.close() and cursor.close(), but I'm wondering if cursor.close() is necessary.

class SQLiteDB(object):
    def __init__(self, dbFile, connect = True):
        self.dbFile = dbFile

        self.con = None
        self.cursor = None

        if connect:
            self.open()

    def __del__(self):
        if self.con:
            self.close()

    def open(self):
        self.con = sqlite3.connect(self.dbFile)
        self.cursor = self.connector.cursor()
        return self.con, self.cursor

    def close(self):
        self.con.close()
        self.cursor.close()
        self.cursor = None
        self.con = None
+1  A: 

What happens on Cursor.close() depends on the underlying database implementation. For SQLite it might currently work without closing, but for other implementations or a future SQLite version it might not, so I would recommend to close the Cursor object. You can find further information on Cursor.close() in PEP 249.

Also, there seems to be a typo in your code:

self.connector = sqlite3.connect(self.dbFile)

should probably be

self.con = sqlite3.connect(self.dbFile)

Otherwise your code looks fine to me. Happy coding :) .

Noya
@Noya : I corrected the typo. Thanks.
prosseek