Skip to content
Matthew Harris edited this page Oct 7, 2013 · 6 revisions
  • More Actions: Raw Text Print View Delete Cache ------------------------ Check Spelling Like Pages Local Site Map ------------------------ Rename Page Delete Page ------------------------ Subscribe User ------------------------ Remove Spam Revert to this revision Package Pages Sync Pages ------------------------ Load Save SlideShow

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](https://github.com/ESGF/esgf.github.io/wiki/) >>> import sqlalchemy, sqlalchemy.orm
[   2](https://github.com/ESGF/esgf.github.io/wiki/) >>> import esgcet.model
[   3](https://github.com/ESGF/esgf.github.io/wiki/) >>> engine = sqlalchemy.create_engine(DB_URL)
[   4](https://github.com/ESGF/esgf.github.io/wiki/) >>> Session = sqlalchemy.orm.sessionmaker(bind=engine)
[   5](https://github.com/ESGF/esgf.github.io/wiki/) >>> 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](https://github.com/ESGF/esgf.github.io/wiki/) >>> q = sess.query(esgcet.model.Dataset).all()
[   2](https://github.com/ESGF/esgf.github.io/wiki/) >>> q[0]
[   3](https://github.com/ESGF/esgf.github.io/wiki/) <Dataset, id=1, name=pcmdi.badc.test.mytest, project='test', model='ncar_ccsm3_0', experiment='test_exp', run_name=None>
[   4](https://github.com/ESGF/esgf.github.io/wiki/) >>> ds = q[0]
[   5](https://github.com/ESGF/esgf.github.io/wiki/) >>> ds.experiment
[   6](https://github.com/ESGF/esgf.github.io/wiki/) 'test_exp'
[   7](https://github.com/ESGF/esgf.github.io/wiki/) >>> ds.name
[   8](https://github.com/ESGF/esgf.github.io/wiki/) 'pcmdi.badc.test.mytest'
[   9](https://github.com/ESGF/esgf.github.io/wiki/) >>> ds.events
[  10](https://github.com/ESGF/esgf.github.io/wiki/) [<Event, datetime=datetime.datetime(2009, 12, 2, 15, 28, 28, 339830), object_id=1, object_name=pcmdi.badc.test.mytest, object_version=1, event=CREATE_DATASET>]
Clone this wiki locally