Re: About mogrify

paftek <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
2008/9/22 Federico Di Gregorio <fog-NGVKUo/i/[email protected]>:
> Il giorno lun, 22/09/2008 alle 12.54 +0200, Oswaldo Hernández ha
> scritto:
>>
>> . Question 2:
>> In an old message (01/30/2007) Federico says:
>>  > is there for DEBUG ONLY. There's really no reason to use .mogrify() directly; .execute() and
>>  > psycopg2.extensions.adapt() should be enough for almost any need.
>>
>> It's safe the use of mogrify() directly?
>
> mogrify() is currently safe but you should not use it anyway because
> there is no guarantee that it will _always_ be safe. .execute()
> and .adapt() will never change semantics and .mogrify() is there only
> because one sometimes need to see a query without looking at the
> database logs (like if you don't have direct shell access to a remote
> server). In theory you don't need .mogrify() at all: if you feel like
> you need it, please explain why and I'll try to find a better way to do
> the same thing or, if mogify() is really needed I'll fix psycopg API to
> support what you need.

Actually I am using mogrify() to generate multi rows INSERT statements
from a list of values :
Is there a better way to do this without mogrify() ?

>>> curs.execute(
...     "INSERT INTO item VALUES %s" % ','.join(
...         curs.mogrify(
...             '%s',
...             [(i, 'Title %d' % i)]
...         )
...         for i in range(10)
...     )
... )
>>> print curs.query
INSERT INTO item VALUES (0, E'Title 0'),(1, E'Title 1'),(2, E'Title
2'),(3, E'Title 3'),(4, E'Title 4'),(5, E'Title 5'),(6, E'Title
6'),(7, E'Title 7'),(8, E'Title 8'),(9, E'Title 9')

Thanks,
Julien
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.