On Apr 24, 2008, at 2:23 AM, Grimsqueaker wrote:
>
> Hi, I'm new to this group - here's my question, I've been fighting
> with this for a while now....
>
> I need to insert a list of lists into a database. It works using a for
> loop but this is very slow so I want to optimize it. The method of
> bulk inserting in SQLAlchemy is much faster but only seems to take a
> list of dictionaries, so I converted the list of lists to a list of
> dictionaries using a list comprehension:
>
> values = [{'name': list[0], 'age': list[1], 'email': list[2]} for list
> in list_of_lists] (the real list is alot longer)
>
> Unfortunately I get an 'index out of range' error when this runs.
>
> Is there any way of doing this more simply? When using the Python
> DBAPI it is as simple as calling executemany(). It seems odd that
> there is not a simple method like that in SQLAlchemy.
This would imply youre using a database that supports positional bind
parameters, so im going to guess MySQL. you can do "straight-to-
DBAPI" executes if you use a string:
engine.execute("insert into table (col1, col2) values (%s %s)",
*list_of_lists)
as far as your "index out of range" error, the code youre using seems
correct so this would imply that one or more of the lists within your
list of lists is not of length three - but that would fail with a
direct DBAPI usage as well since one bind parameter would be left
unfulfilled.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---