1

I have a program which calculates a set of plain interlinked objects (the objects consist of properties which basically are either String, int or link to another object). I would like to have the objects stored in a relational database for easy SQL querying (from another program).

Moreover, the objects (classes) tend to change and evolve. I would like to have a generic solution not requiring any changes in the 'persistence layer' whenever the classes evolve.

Do you see any way to do that?

4 Answers 4

1

What about storing the objects in JSON? You could write a function that serialize your object before storing it into the database. If you have a specific identifier for your objects, I would suggest to use it as index so that you can easily retrieve it.

Sign up to request clarification or add additional context in comments.

Comments

1

I haven't used it myself, but I this sounds like use case for SQLAlchemy:

http://www.sqlalchemy.org/

It basically let's you define object-properties as table-columns or relations to other classes and takes care of basically all direct db-interaction. There might be other libraries out there doing the same, but SQLAlchemy is the one I once stumbled over and seems to be pretty commonly used.

2 Comments

I haven't tried in, correct me if I am wrong, but it looks like SQLAlchemy makes me declare all the properties/columns explicitly. That is using Column('name', Type) stanza.
Well, yes, you'll have to declare all properties that are supposed to be stored in the tables explicitly - i don't think there's way around this. You don't have to invent column-names though, as far as I can see: docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html
0

sebastian's idea is probably the easiest to use, since it does the object mapping for you, however, if you are already familiar with SQL, and prefer that, and are using python 2.5 or later, you might consider the sqlite3 library. It's a very easy to use library, and I'm pretty sure it doesn't require any work to set it up. You might have to have the sqlite3 libraries installed, though. http://docs.python.org/2/library/sqlite3.html

Comments

0

SQLAlchemy is great. Another leading option is using the ORM from django:

https://docs.djangoproject.com/en/dev/intro/tutorial01/#creating-models

Basically creating classes in python which extend the django model class are mapped to standard relational database tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.