Skip to content
Stephen Pascoe edited this page Apr 9, 2014 · 6 revisions

Connect to the data model using SQLAlchemy

You will need your SQLAlchemy database connection URL which can be found in your ~/.esgcet/esg.ini file as the option dburl .

_ Note this code was run using esgcet-2.0 _

[   1] >>> import sqlalchemy, sqlalchemy.orm
[   2] >>> import esgcet.model
[   3] >>> engine = sqlalchemy.create_engine(DB_URL)
[   4] >>> Session = sqlalchemy.orm.sessionmaker(bind=engine)
[   5] >>> sess = Session()

Querying a dataset

You can now query the database by asking for ORM classes defined in esgcet.model . For instance to read all datasets into a list and interrogate the first dataset do:

[   1] >>> q = sess.query(esgcet.model.Dataset).all()
[   2] >>> q[0]
[   3] >>> ds = q[0]
[   5] >>> ds.experiment
[   6] 'test_exp'
[   7] >>> ds.name
[   8] 'pcmdi.badc.test.mytest'
[   9] >>> ds.events
[  10], object_id=1, object_name=pcmdi.badc.test.mytest, object_version=1, event=CREATE_DATASET>]

Clone this wiki locally