Reputation: 13758
I am looking for a simple and efficient way to get a custom object from sqlite db.
There is some moudle provide similar function such as sqlobject
.
But my purpose is very simple:
I want to save data with normal sql statement like cur.execute("create table point(x INTEGER,y INTEGER)")
and get data by cur.execute('select x,y as "p [Point]" from point')
,
Then cur.fetchone()[0]
will return a Point
object.
Sqlite official document has a way to save and retrieve an object at here.
They register a adapter and a converter for Point class which does not correspond my aim only use the converter.
In simple words,I don't want to register a adapter in sqlite. Register a custom type will make it difficult to query a metadata, so I just want to convert the data to a custom type when I retrieve data.Of course,I can use a custom function to put them into custom object after I get the data .But I don't think this way is so simple and efficient.
So, what is the best way to solve this problem simply and efficiently?
Upvotes: 1
Views: 1604
Reputation: 129001
Use an ORM. There are several available for Python. A notable one is SQLAlchemy.
Upvotes: 2