Wednesday, January 6, 2010

SQLAlchemy and Rdbhost

SQLAlchemy is an ORM, an object relational mapper. To some folks, that is a very meaningful phrase. To me, it is just another way to programmatically access SQL databases.

This week, I created a module to facilitate using Rdbhost databases from SQLAlchemy. See it documented on: http://www.rdbhost.com/orms.html . We provide one file, aptly called 'rdbhost.py' which you add to your SQLAlchemy's 'database' directory, and SQLAlchemy will then use it to open and access databases hosted on www.rdbhost.com.

When you call the create_engine function, you give it a connect string like: 'rdhbost://role:authcode@www.rdbhost.com' .

Once you have the engine object, thus created, you can create, drop, and query tables using the SQLAlchemy/Python syntax.

Here is an excerpt:

from sqlalchemy import *
role = 's0000000849'
authcode = 'h.89as9fa9dsaf.~~~~~~~~~~kajd8098ajsf'
dsn = 'rdbhost://'+role+':'+authcode+'@www.rdbhost.com'
db = create_engine(dsn)

metadata = MetaData()
users = Table('Users', metadata,
... Column('id', Integer, primary_key=True),
... Column('name', String),
... Column('fullname', String),
... )
addresses = Table('addresses', metadata,
... Column('id', Integer, primary_key=True),
... Column(user.id', None, ForeignKey('users.id')),
... Column('email_address', String, nullable=False)
... )
metadata.create_all(db)

No comments:

Post a Comment