Hi *,
I am wondering if there is a way to generate an insert from select with
SQLAlchemy. Consider this code:
from sqlalchemy import *
engine = create_engine("sqlite:///demo.ldb", echo=True)
md = MetaData()
users = Table("users", md,
Column("id", Integer, primary_key=True),
Column("name", String),
Column("status", Integer))
md.create_all(engine)
engine.execute(users.insert().values(name="Joe Sixpack", status=1))
engine.execute(users.insert().values(name="Jane Citizen", status=1))
# insert from select?
# engine.execute(users.insert().values(
# users.select([users.c.name]), status=2))
conn = engine.connect()
with conn.begin():
for row in conn.execute(users.select()).fetchall():
conn.execute(users.insert().values(
name=row["name"], status=2))
Is there a way to generate the "natural" SQL for this:
insert into users (name, status) select name, 2 as status from users
Basically I would like to tell SQLAlchemy to use a query to provide the
values for insert. StackOverflow says this can't be done, but I can't
believe that. :-)
Greetings, Torsten
--
DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
Torsten Landschoff
Office Dresden
Tel: +49-(0)351-4519587
Fax: +49-(0)351-4519561
mailto:[email protected]
http://www.dynamore.de
Registration court: Mannheim, HRB: 109659, based in Karlsruhe,
Managing director: Prof. Dr. K. Schweizerhof, Dipl.-Math. U. Franz
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en.