#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Class variables for sqlite


    Hi!

    This should be an easy question, I am trying to make a class for managing sqlite connections with python.

    I have this so far (not to much )

    Code:
    import sqlite3
    
    class Engine:
      connection = None
      cursor = None
      def __init__(self, db_name, db_user):
        self.connection = sqlite3.connect(db_name)
        self.cursor = connection.cursor()
        self.cursor.execute('SELECT * FROM questions');
    
      def __del__(self):
        self.cursor.close()
        self.connection.close()
    I have tried to create the __del__ method as a destructor, in order to get rid of the connection with the database.

    But I don't know how to declare those two variables as class variables in order to be used by the methods.

    This is the error I get by creating an instance of the "Engine" class:
    NameError: global name 'connection' is not defined
    Exception AttributeError: "'NoneType' object has no attribute 'close'" in <bound method Engine.__del__ of <Engine.Engine instance at 0x7fb3a3e70cf8>> ignored


    Any idea would be appreciated!

    Thank you in advance!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I suggest you try
    http://pypi.python.org/pypi/pysqlite/
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Still a better idea, you won't be tied to a specific back end data base, I think,
    http://wiki.python.org/moin/ODBC
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo