session.execute w/ list of values instead of dict?

1,338 views
Skip to first unread message

Wells Oliver

unread,
Jun 23, 2011, 5:08:57 PM6/23/11
to sqlalchemy
W/ psycopg2, you can do a cursor.execute(query, list) where list is an
actual python list of values : [1,2,3]

W/ SQLAlchemy, it seems the session.execute(query, values) will only
accept a dictionary for values. Am I missing something? Can I pass a
list instead? Thanks.

Michael Bayer

unread,
Jun 23, 2011, 5:48:51 PM6/23/11
to sqlal...@googlegroups.com
I was a little surprised the docs don't refer to the fact that you should use connection() for this case, so I updated them:


"If a plain string is passed, it is first converted to a text() construct, which here means that bind parameters should be specified using the format :param. If raw DBAPI statement execution is desired, use Session.connection() to acquire a Connection, then call its execute() method."


Wells Oliver

unread,
Jun 23, 2011, 5:54:14 PM6/23/11
to sqlalchemy
Hmm, new to SQLAlchemy here, but if I want transactions, then I need
to go the Session route, correct?

On Jun 23, 2:48 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
> On Jun 23, 2011, at 5:08 PM, Wells Oliver wrote:
>
> > W/ psycopg2, you can do a cursor.execute(query, list) where list is an
> > actual python list of values : [1,2,3]
>
> > W/ SQLAlchemy, it seems the session.execute(query, values) will only
> > accept a dictionary for values. Am I missing something? Can I pass a
> > list instead? Thanks.
>
> I was a little surprised the docs don't refer to the fact that you should use connection() for this case, so I updated them:
>
> http://www.sqlalchemy.org/docs/orm/session.html#sqlalchemy.orm.sessio...http://www.sqlalchemy.org/docs/core/connections.html#sqlalchemy.engin...

Michael Bayer

unread,
Jun 23, 2011, 6:05:17 PM6/23/11
to sqlal...@googlegroups.com

If you are using Session.connection() with the ORM, it gives you a connection that's part of the Session's transaction.

If you aren't working with the ORM and just have an Engine, you need to get a Connection then call begin() on it.

this stuff is all up there if you poke around a bit.

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

Reply all
Reply to author
Forward
0 new messages