Ok thanks I got it! On Sun, Aug 1, 2010 at 11:32 AM, sathya r <[email protected]>wrote:
> Hi thanks! I guess I solved that but again another very newbie thing:- > How would i query a database having a table city.. Class=City , table > rows(id,name,countryid) > I did this which I presume is correct:- > city_query=DBSession.query(City).filter_by(countryid=1) > Now how to I iterate through this... kindly help... > Thanks > > > On Sun, Aug 1, 2010 at 10:00 AM, Wesley Dyk <[email protected]> wrote: > >> On Sat, Jul 31, 2010 at 1:49 PM, sathya <[email protected]> >> wrote: >> > Hi this is a very naive question, but please help a newbie:- >> > I have modeled two classes for the database like this:- >> > >> > class City(DeclarativeBase): >> > __tablename__='citytable' >> > id=Column(Integer,primary_key=True) >> > name=Column(Unicode(50),nullable=False) >> > >> > country_id=Column(Integer,ForeignKey('countrytable.id'),nullable=False) >> > country_name=relation('Country',foreign_keys=country_id) >> > >> > >> > class Country(DeclarativeBase): >> > __tablename__='countrytable' >> > id=Column(Integer,primary_key=True) >> > countryname=Column(Unicode(50),nullable=False) >> > >> > Now if I have to select all cities from a country say US, am I not >> > supposed to do this. >> > citylist=[city.name for city in City.filter_by(name='US')] >> > >> > and can someone also please help me know how to use sqlalchemy >> > through CLI. >> > I used the following codes:- >> > city_q=DBSession.query(City) >> > however when I give city_q.all().. I get :- >> > sqlalchemy.exc.UnboundExecutionError: Could not locate a bind >> > configured on mapper Mapper|City|citytable >> > >> > >> > Please help! >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups "TurboGears" group. >> > To post to this group, send email to [email protected]. >> > To unsubscribe from this group, send email to >> [email protected]<turbogears%[email protected]> >> . >> > For more options, visit this group at >> http://groups.google.com/group/turbogears?hl=en. >> > >> > >> >> The problem there is that you have not prepared your database engine >> (connection). If you have your TG app setup correctly, you can use >> "paster shell development.ini" (or whatever your ini file is named) >> because that shell initializes your app for you. If it is not setup, >> then you will have to use sqlalchemy's mechanics yourself to connect >> to the database. In config/app_cfg.py is the code TG uses to setup >> the database connection and the call to your model.init_model function >> (in model/__init__.py) to prepare DBSession and DeclarativeBase. You >> can look at those files to see how it is done. Rather than a call to >> engine_from_config, you can just pass the database URI string to >> sqlalchemy.create_engine and bind DBSession to the returned object. >> After that, you can query your model. >> >> Wes >> >> -- >> You received this message because you are subscribed to the Google Groups >> "TurboGears" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<turbogears%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/turbogears?hl=en. >> >> > -- You received this message because you are subscribed to the Google Groups "TurboGears" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.

