The examples below are from the official SQLAlchemy page. If anything is wrong, incorrect, or simply can be done in less code SQLAlchemy please let me know and I will fix.
SQLite, MySQL, PostgreSQL, Oracle, MSSQL(*), FireBird, Informix, DB2
SQLite, MySQL, PostgreSQL, Oracle, MSSQL, FireBird, DB2, Informix, Ingres, Google App Engine(2)
(1) = no OR, LIKE, JOIN, nested selects
>>> from sqlalchemy import *
>>> from sqlalchemy.sql import select
>>> engine = create_engine('sqlite://file.db')
>>> from gluon.sql import *
>>> db=DAL('sqlite://file.db')
>>> metadata = MetaData()
>>> users = Table('users', metadata,
... Column('id', Integer, primary_key=True),
... Column('name', String),
... Column('fullname', String),
... )
>>> addresses = Table('addresses', metadata,
... Column('id', Integer, primary_key=True),
... Column('user_id', None, ForeignKey('users.id')),
... Column('email_address', String, nullable=False)
... )
>>> metadata.create_all(engine)
>>> db.define_table('users',Field('name'),Field('fullname'))
>>> db.define_table('addresses',Field('user_id',db.user),Field('email_address',notnull=True))
Notes:
One can define
users=db.users
addresses=db.addresses
>>> ins = users.insert(values={'name':'jack', 'fullname':'Jack Jones'})
>>> str(ins)
'INSERT INTO users (name, fullname) VALUES (:name, :fullname)'
>>> conn = engine.connect()
>>> result = conn.execute(ins)
>>> result.last_inserted_ids()
1
>>> id=db.users.insert(name='jack', fullname='Jack Jones')
>>> repr(db._lastsql)
"INSERT INTO users (name, fullname) VALUES ('jack','Jack Jones')"
>>> id
1
>>> s = select([users])
>>> result = conn.execute(s)
>>> for row in result:
... print row
(1, u'jack', u'Jack Jones')
(2, u'wendy', u'Wendy Williams')
(3, u'fred', u'Fred Flintstone')
>>> s = db().select(db.users.ALL)
>>> for row in result:
... print row.id, row.name, row.fullname
1 jack Jack Jones
2 wendy 'Wendy Williams
3 fred Fred Flintstone
>>> row = result.fetchone()
>>> print "name:", row['name'], "; fullname:", row['fullname']
name: jack ; fullname: Jack Jones
>>> row = result[0]
>>> print "name:", row['name'], "; fullname:", row['fullname']
name: jack ; fullname: Jack Jones
>>> s = select([users.c.name, users.c.fullname])
>>> result = conn.execute(s)
>>> for row in result:
... print row
(u'jack', u'Jack Jones')
(u'wendy', u'Wendy Williams')
(u'fred', u'Fred Flintstone')
(u'mary', u'Mary Contrary')
>>> db().select(db.users.name,db.users.fullname)
>>> for row in rows: print row.name, row.fullname
jack Jack Jones
wendy 'Wendy Williams
fred Fred Flintstone
Notes:
>>> s = select([users, addresses], users.c.id==addresses.c.user_id)
sql>>> for row in conn.execute(s):
... print row
(1, u'jack', u'Jack Jones', 1, 1, u'jack@yahoo.com')
(1, u'jack', u'Jack Jones', 2, 1, u'jack@msn.com')
(2, u'wendy', u'Wendy Williams', 3, 2, u'www@www.org')
(2, u'wendy', u'Wendy Williams', 4, 2, u'wendy@aol.com')
>>> for row in db(db.users.id==db.addresses.user_id).select():
... print row.users.name, row.addresses.email_address
jack jack@yahoo.com
jack jack@msn.com
wendy www@www.org
wendy wendy@aol.com
>>> print users.c.name.like('j%') & (users.c.id==addresses.c.user_id) & \
... ((addresses.c.email_address=='wendy@aol.com') | \
... (addresses.c.email_address=='jack@yahoo.com')) \
... & ~(users.c.id>5)
users.name LIKE :name_1 AND users.id = addresses.user_id AND
(addresses.email_address = :email_address_1 OR addresses.email_address = :email_address_2)
AND users.id <= :id_1
>>> print db.users.name.like('j%') & (db.users.id==db.addresses.user_id) & \
... ((db.addresses.email_address=='wendy@aol.com') | \
... (db.addresses.email_address=='jack@yahoo.com')) \
... & ~(db.users.id>5)
users.name LIKE 'j%; AND users.id = addresses.user_id AND
(addresses.email_address = 'wendy@aol.com' OR addresses.email_address = 'jack@yahoo.com' AND users.id <= :id_1
>>> s = select([users.c.fullname,addresses.email_address],\
... from_obj=[users.outerjoin(addresses, addresses.c.user_id==users.c.id)])
>>> for row in conn.execute(s): print row
(u'Jack Jones', u'jack@yahoo.com')
(u'Jack Jones', u'jack@msn.com')
(u'Wendy Williams', u'www@www.org')
(u'Wendy Williams', u'wendy@aol.com')
(u'Fred Flintstone', None)
(u'Mary Contrary', None)
>>> rows=db().select(db.users.fullname, db.addresses.email_address, \
... left=db.addresses.on(db.addresses.user_id==db.users.id))
>>> for row in rows: print row.users.fullname, row.addresses.email_address
Jack Jones jack@yahoo.com
Jack Jones jack@msn.com
Wendy Williams www@www.org
Wendy Williams wendy@aol.com
Fred Flintstone None
Mary Contrary None
...
...
...
...
I am tired ... I will finish this some other time....