The insert is looking for a list of dictionaries. What you would like to do
is enter column names once along with lists representing rows of data to
insert.

Here is some code I use to simplify setting up the dictionaries when
inserting a large number of rows. I'm sure others have come up with other
approaches.


"""==========================================
Build list of dictionaries for insert operation
==========================================="""
cols = ('deptid','deptname',)
data = ((31,'Sales'),(33,'Engineering'),)
insdata = [dict(zip(cols,datum)) for datum in data]
conn.execute(dept.insert(), insdata)
## or in a single statement ##
conn.execute(dept.insert(), [
    dict(zip(('deptid','deptname',), datum)) for datum in (
            (31,'Sales'),
            (33,'Engineering'),
        )]


-- 
Mike Conley



On Tue, Jun 9, 2009 at 9:31 AM, Michael Bayer <mike...@zzzcomputing.com>wrote:

>
> "normal" SQL includes named bind parameters.  you might be looking for a
> raw execution, i.e.:
>
> conn.execute("insert into table (a, b, c) values (?, ?, ?)", [(1,2,3),
> (4,5,6)])
>
>
> that will take you right through to the DBAPI but your code will not be
> database agnostic.
>
> Ashish Bhatia wrote:
> >
> > yeah thnks for the help.
> >
> > one more dbt. do i have to provide values as dict always ie. with
> > column name. Can i give direct values like we do in normal sql.?
> > thnks in advance
> >
> > On Jun 9, 9:02 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> >> you need to use the executemany form described
> >> athttp://
> www.sqlalchemy.org/docs/05/sqlexpression.html#executing-multip...
> >>   .
> >>
> >> On Jun 8, 2009, at 11:54 PM, Ashish Bhatia wrote:
> >>
> >>
> >>
> >>
> >>
> >> > Sorry, Its my typing mistake :( . I put : insted , . But still my
> >> > question yeat remained unanswered. :(
> >>
> >> > On Jun 8, 7:50 pm, Didip Kerabat <did...@gmail.com> wrote:
> >> >> You have Syntax Error here:
> >>
> >> >> ('sdsd':'sdsds')
> >>
> >> >> That one should be tuple right?
> >>
> >> >> - Didip -
> >>
> >> >> On Mon, Jun 8, 2009 at 6:14 AM, Ash <ashishsinghbha...@gmail.com>
> >> >> wrote:
> >>
> >> >>> Hello ,
> >>
> >> >>> I am trying to insert in the table using two ways in the values
> >> >>> which
> >> >>> i show below
> >>
> >> >>> engine = sqlalchemy.create_engine(<to poastgres>)
> >> >>> metadata = MetaData()
> >>
> >> >>> t1 = Table('master',metadata) # assume master has 2 feilds name ,
> >> >>> city
> >>
> >> >>> t1.insert({'name':'tttt','city':'bank'})
> >>
> >> >>> engine,execute(t1)
> >>
> >> >>> This works for fine me.
> >>
> >> >>> If i make values like this
> >> >>> tt = [('asasas','belhium'),('sdsd':'sdsds')]
> >>
> >> >>> t1.insert(values=tt)
> >>
> >> >>> i get error
> >> >>> sqlalchemy.exceptions.ProgrammingError: (ProgrammingError) syntax
> >> >>> error at or near ")"
> >> >>> LINE 1: INSERT INTO abc () VALUES ()
> >> >>>                                ^
> >> >>>  'INSERT INTO abc () VALUES ()' {}
> >>
> >> >>> Can any one guide whts wrong... i jnow  value is not being passed so
> >> >>> anyother way.
> > >
> >
>
>
> >
>

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

Reply via email to