problem with quoted strings while inserting into varchar field of database.

Stefan Sonnenberg-Carstens stefan.sonnenberg at pythonmeister.com
Mon May 7 09:55:33 EDT 2007


On Mo, 7.05.2007, 11:32, Daniele Varrazzo wrote:
> On 7 Mag, 10:46, "Stefan Sonnenberg-Carstens"
> <stefan.sonnenb... at pythonmeister.com> wrote:
>> On Mo, 7.05.2007, 10:30, Daniele Varrazzo wrote:
>>
>> > On 7 Mag, 08:55, "krishnakant Mane" <researchb... at gmail.com> wrote:
>> >> On 6 May 2007 11:22:52 -0700, Daniele Varrazzo
>> >> <daniele.varra... at gmail.com> >> Every serious database driver has a
>> >> complete and solid SQL escaping
>> >> > mechanism. This mechanism tipically involves putting placeholders
>> in
>> >> > your SQL strings and passing python data in a separate tuple or
>> >> > dictionary. Kinda
>>
>> >> >     cur.execute("INSERT INTO datatable (data) VALUES (%s);",
>> >> > (pickled_data,))
>>
>> >> I will try doing that once I get back to the lab.
>> >> mean while I forgot to mention in my previous email that I use
>> MySQLdb
>> >> for python-mysql connection.
>>
>> Why not use qmark parameter passing (PEP 249) ?
>>
>> cur.execute("INSERT INTO datatable (data) VALUES (?);" ,
>> (pickled_data,))
>>
>> Then the DB driver will take care for you.
>
>>>> import MySQLdb
>>>> print MySQLdb.paramstyle
> format
>
> MySQLdb (as many other drivers) use format parameter passing. Not much
> difference w.r.t. qmark, at least when passing positional parameters:
> the placeholder is "%s" instead of "?". A difference is that "format"
> also allows named parameters (actually it should have been "pyformat",
> but IIRC MySQLdb can also use named placeholders, even if they
> advertise "format").
>
> Anyway it is only a matter of placeholder style: they both allow the
> driver to take care of data escaping, the concept the OT didn't know
> about.
>
> -- Daniele
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
%s is not a placeholder IMHO.
What happens when using %s is, that the string given will be inserted where
%s is; that is something python does as with every print or such.
By using the qmark style, it is up the the implementation of the
cursor.execute method to decide what to do. python itself, and it's string
implementation, don't know anything to do with the qmark.
So, IMHO it *makes* a difference:
with %s the execute function sees a string and nothing more as the
parameters are consumed away by the % substitution.
with ?, the execute implementation must do it's best, it gets a string and
a list/tuple with values.

Cheers,
Stefan



More information about the Python-list mailing list