
December 2nd, 2012, 01:56 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 1
Time spent in forums: 7 m 17 sec
Reputation 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!
|