I\'m using sqlite3 in python 2.5. I\'ve created a table that looks like this:
create table votes (
bill text,
senator_id text,
vote text)
<
I use something like this:
class SqliteRow(object):
def __init__(self):
self.fields = []
def add_field(self, name, value):
self.fields.append(name)
setattr(self, name, value)
def to_tuple(self):
return tuple([getattr(self, x) for x in self.fields])
with this:
def myobject_factory(cursor, row):
myobject= MyObject()
for idx, col in enumerate(cursor.description):
name, value = (col[0], row[idx])
myobject.add_field(name, value)
return myobject
MyObject()
is a class that inherits from SqliteRow
.
SqliteRow class is a base class for every object that I want to have returned by a query.
Every column becomes an attribute and is logged into the fields
list.
Function to_tuple
is used to change the whole object to a form suitable for queries (simply pass the whole object and forget).
To get different class types of that function. You would need to make a factory object, that will generate objects based on the list of fields (for example: dict with { some_unique_value_made_of_fields: class} )
This way I get a simple ORM.