Trouble with Myqsl

Steve Holden sholden at holdenweb.com
Wed Apr 24 08:21:56 EDT 2002


"Steffen Kirschke" <steffen at molli15.org> wrote ...
> Gerhard Häring wrote:
>
> > Fernando Rodríguez wrote in comp.lang.python:
> >>
> >> I'm using CompatMysqldb from
http://sourceforge.net/projects/mysql-python
> >> with Python 2.2
> >>
> >> Whenever I try to insert some data into a mysql db, I get this error:
> >>
> >>>>> curs.execute("insert into customer_info (email, url, referrer,
> >>>>> first_visit) values ( '%s', '%s', '%s', '%s')",
> >>>>> [('billg at microsoft.com', 'www.easyjob.net', 'www.google.com',
> >>>>> '2001-03-07 12:32:00')])
> > AFAIC, you should be using a tuple as second parameter to execute, not
> > a list containing a tuple.
>
> and also use a '%' instead of the ','.
>
> curs.execute("""insert into table (col1, col2,...) values ('%s',
>         '%s',...)""" % (val1, val2, ...)
>

Please don't do that unless you have to (which you sometimes do if it's a
table that needs to vary between queries, for example).

The original example uses a parameterized SQL query. This means that the SLQ
engien has a chance to optimize the query independent of the actual data
values used, which in turn can speed up your application if the same query
is repeatedly used with different data.

Your version (which I used to suggest myself, so it's not like I'm accusing
you of a crime <wink>) generates a unique SQL statement which is guranteed
to be different from the next, and the one after that, depriving the SQL
implementation of the chance to optimize.

For completeness, I will repeat that *most* DBAPI-compliant modules '
execute() method will accept a list of tuples as data, repeating the query
with each tuple. Nowadays one should really explicitly use the executemany()
cursor method for such purposes, but befioer it was available execute() was
often written to accept a list of tuples.

regards
 Steve
--

home: http://www.holdenweb.com/
Python Web Programming:
http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list