Help with MySQLdb insert with data extracted from odbc <-- Oracle

Steve Holden sholden at holdenweb.com
Tue Jun 25 00:09:08 CEST 2002


<candiazoo at attbi.com> wrote ...
> Ah!  Thank you!
>
> Mike J.
>
> On Sat, 22 Jun 2002 04:17:58 +0200, Gerhard =?iso-8859-15?Q?H=E4ring?=
> <gerhard.haering at gmx.de> wrote:
>
> >* candiazoo at attbi.com <candiazoo at attbi.com> [2002-06-22 01:43 +0000]:
> >> Can someone tell me what this means?  I mean, I guess I *know* what it
means,
> >> but why?
> >>
> >> The insert statement into a mysql database is...
> >>
> >> 'insert into works values(?, ?, ?, ?, ? ,?)'
> >
> >Wrong. paramstyle = 'format' in MySQLdb, not 'qmark'. Look into the
> >DB-API specs (PEP 0249) for details about the various paramstyles that
> >are possible.
> >
> >> The invocation of the dynamic sql is...
> >>
> >>     status = cur_mysql.execute(sql,
> >>         (wrk_inst, ZERO, trs_inst, aas_inst, title, NADA))
> >
> >Simply use %s instead of ?. And read the fine MySQLdb docs that are
> >included :-)
> >

Please also note that you are potentially asking for trouble by assuming a
particular order of the fields in your table definition. It would be *much*
safer (though a little more tedious) to write

cursor.execute("""INSERT INTO
        wrk_inst, sinker, trs_inst, aas_inst, title, pub_name)
        VALUES (%s, %s, %s, %s, %s, %s)""")

That way, when someone else appears in a year or two and jiggles around with
the table definition it's less likely your code will blow up because you are
trying to insert 'N/A' into a numeric field.

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list