1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| from sqlalchemy import select, Table, MetaData, insert, func
t_foo = Table("foo", MetaData(), autoload_with=db)
with db.begin() as conn: sql = select(t_foo.c.id.label('gg'), t_foo.c.name).filter_by(id=1).order_by(t_foo.c.id.desc()) print(sql) print(list(map(dict, conn.execute(sql).mappings().all())))
conn.execute(insert(t_foo).values(id=2)) conn.execute(insert(t_foo), [{"id": 3, "name": 'wang'}, {"id": 4, "name": 'li'}, {"id": 4, "name": 'sun'}])
conn.execute(delete(t_foo).where(t_foo.c.id.in_((2, 3))))
conn.execute(update(t_foo).where(t_foo.c.name.contains('a')).values(name='aaaa'))
sql = select(t_foo.c.id, func.count(t_foo.c.id).label("cnt"), func.max(t_foo.c.id)).group_by(t_foo.c.id)
|