I just started playing with sqlalchemy. At the moment I am working
with one database, but properly I need to work with severall database
at the same time, but I want to work in such a way, that I do not need
to know in which database a certain table resides.

Is this possible?
For example it looks like it is possible for the session object.
I use in a model file (through an import):
#####
import sqlalchemy     as sa
import sqlalchemy.orm as sa_orm

metadata = sa.MetaData('sqlite:////home/cecil/databases/metingen.db')
engine   = metadata.bind
Session  = sa_orm.sessionmaker()
session  = Session()

gewichtTable = sa.Table(
  'gewicht', metadata,
  sa.Column('id',      sa.Integer, primary_key = True),
  sa.Column('datum',   sa.Date,  nullable = False, unique = True),
  sa.Column('gewicht', sa.Float, nullable = False),
  sa.Column('vet',     sa.Float, nullable = False),
  sa.Column('water',   sa.Float, nullable = False),
  sa.Column('spieren', sa.Float, nullable = False),
)
class Gewicht(object):
  def __repr__(self):
    return '<Gewicht: %10.10s,  %5.1f, %5.1f, %5.1f, %5.1f>' %
(self.datum, self.gewicht, self.vet, self.water, self.spieren)
sa_orm.mapper(Gewicht, gewichtTable)
#####

In the main program I have:
#####
import sqlalchemy     as sa
import sqlalchemy.orm as sa_orm

Session  = sa_orm.sessionmaker()
session  = Session()

from metingen_model  import Gewicht, gewichtTable
#####

And later on I do:
#####
for row in session.query(Gewicht).all():
#####

And this does what it should do. So I expect that the engine is
fetched out of Gewicht.

First I had:
        for row in engine.execute('SELECT MIN(gewicht) AS gewicht '
                                  ',      MIN(vet) AS vet '
                                  ',      MAX(water) AS water '
                                  ',      MAX(spieren) AS spieren '
                                  'FROM   gewicht '):
but because I want it to have the code engine independend I changed that to:
        for row in sa.select([sa.func.min(Gewicht.gewicht)
                               , sa.func.min(Gewicht.vet)
                               , sa.func.max(Gewicht.water)
                               , sa.func.max(Gewicht.spieren)]).execute():
The problem with this is that I now need to use row[0] instead of
row.gewicht. Is there a way to use row.gewicht also in the case
sa.select?

Is this a good way of doing things, or should I work with severall engines?

Also sqlalchemy and sqlalchemy.orm are now imported in the main
program and in metingen_model. And later on they will become imported
in even more modules. Is this a problem? I was told that Python was
intelligent enough to see that sqlalchemy and sqlalchemy.orm are
allready imported and that importing again should not have
-significant- consequences. Is that true?

-- 
Cecil Westerhof

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to