Sqlalchemy

Connection

Here is the example mysql connection. Set that up in development.ini and pick what database you want to use.

sqlalchemy.url=mysql://user:pass@localhost/mydatabase
or 
sqlalchemy.url=mysql://user:pass@localhost/mydatabase?charset=utf8

Query, group by

In this example I query the Recall Table that I defined in model code. This one is autoloaded.

        if year=='':
            pylons.c.years=DBSession.query(Recall.YEARTXT).group_by(Recall.YEARTXT)
        elif make=='':
            pylons.c.makes=DBSession.query(Recall.MAKETXT).filter(Recall.YEARTXT==year).group_by(Recall.MAKETXT)
        elif model=='':
            pylons.c.models=DBSession.query(Recall.MODELTXT).filter(and_(Recall.YEARTXT==year,Recall.MAKETXT==make)).group_by(Recall.MODELTXT)
        else:
            pylons.c.data=DBSession.query(Recall).filter(and_(and_(Recall.YEARTXT==year,Recall.MAKETXT==make),Recall.MODELTXT==model))

MyWiki: TurboGears2/Sqlalchemy (last edited 2009-09-27 01:14:11 by LukaszSzybalski)