I have read this question but it isn't clear to me. I defined my class like this:
from sqlite3 import Connection, Cursor, Row, connect
class myclass(object):
def __init__(self,number):
co = connect('C:\\mydatabase.sqlite')
co.row_factory = Row
with connection:
cu = connection.cursor()
sql = '''SELECT * FROM mytable WHERE Number= {n} LIMIT 1'''.format(n = number)
cu.execute(sql)
for i in cu:
self.classattribute1 = i['Field1']
self.classattribute2 = i['Field2']
etc.
Now this works fine until I want to add a third attribute to my class like:
self.classattribute3 = self.classattribute1 + self.classattribute2
AttributeError: 'myclass' object has no attribute 'classattribute1'
This won't work if the SELECT
statement didn't return anything, if the number isn't in the database.
Now what I would like to do when I call an instance of myclass like:
myclassinstance1 = myclass(100)
I would like to write something like:
if cu.fetchone() == None:
#code to exit the method __init__ and to delete my class instance here
I don't know how to exit and delete the instance I called from inside myclass
. I need to delete theses instances because I don't want to use empty class instances.
Thanks for reading.