On Tuesday, June 3, 2014 10:52:34 PM UTC-4, Michael Bayer wrote:
> traditional multi-insert is to use executemany() syntax, I’m not sure if
> sqlite supports values () (), anyway.
>
>
Not sure about that either. The sqlite example was just quick way how to
reproduce something resembling the actual problem I had in Postgres.
> data = [
> {"id": 1, "value": "foo"},
> {"id": 2, "value": "bar"}
> ]
>
> stmt = table.insert().values(adate=sql.func.now())
> engine.execute(stmt, data)
>
>
Thanks, this worked.
In this case I was solving a batch/buffered INSERT for ETL of bulk data and
tried to have it more generic. Just looks a bit "hacky" in the code, as
there are two places where the records are populated with values: one is
for actual data the other is for the timestamp (we are going to have few
more in the future).
>
> the func.now() is a constant and the multi-values thing doesn’t support
> different constant expressions in each bundle as it would mean lots of
> expensive isinstance() checks,
>
Understood. The costs reasons make sense.
if you just put it in the first element that will work also:
>
>
> data = [
> {"id": 1, "value": "foo", "adate":sql.func.now()},
> {"id": 2, "value": "bar"}
> ]
>
> stmt = table.insert().values(data)
> print stmt
>
>
>
Thanks a lot for the help.
Stefan
>
>
>
>
> On Jun 3, 2014, at 5:40 PM, Stefan Urbanek <[email protected]
> <javascript:>> wrote:
>
> Hi,
>
> I'm trying to multi-insert records which contain a function:
>
> record = {
>
> "some_date": sql.func.now()
>
> key: value, ...
>
> }
>
>
> When I execute table insert with list of records where len(records) > 1
> then I get an error:
>
> “sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type
> 'now’”
>
>
> I noticed something in the string representation of the statement:
>
> INSERT INTO table (some_date, col1, col2) VALUES (now(), %(col1_0)s,
> %(col2_0)), (%(some_date_1)s, %(col1_1)s, %(col2_1))
>
>
> Note that the first value in the second record is `%(some_date_1)s`
> instead of `now()`
>
> What might be wrong?
>
> Stefan
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.