Re: executemany question

Gerhard Häring <[email protected]>
Newsgroups gmane.comp.python.db.pypgsql.user
Message-ID <[email protected]>
Steve Waterbury wrote:
> I may be misunderstanding the purpose of
> cursor.executemany(query, sequence_of_params) ...
> 
> The doc says "Execute a query many times, once
> for each params in the sequence."  I had hoped
> the results would accumulate in the cursor, but
> it doesn't appear to (perhaps I'm doing something
> wrong!).  Here's what I'm doing:
> 
> curs.executemany('select * from docs where doc_number = %s', docnums)

executemany is not supposed to be used with SELECTs. Use it for INSERT 
or UPDATE only.

> in which docnums is a list of strings, which are
> values of doc_number.  (I've verified that they are
> valid.)  Then when I then do
> 
> curs.fetchall()
> 
> ... I get only the result of the execution of the query
> using the last item in the list (which is certainly what
> you get by doing several "execute(query, parms)"
> in succession).
> 
> Is this the way it's supposed to work?
> 
> And if so, what would be a good way of doing what I want to
> do (i.e., feed in a sequence of parameters and get
> back a cumulative result of executing the same
> query with each one)? [...]

In your particular example:

docnums = [3,4,5,6]
curs.execute("select * from docs where doc_number in %s", (docnums,))

Other problems will require other solutions.

-- Gerhard


-------------------------------------------------------
This SF.net email is sponsored by:  Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.