472,337 Members | 1,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

DB-API corner case (psycopg2)

Hi,

I discovered this:

import psycopg2
connection=psycopg2.connect("dbname='...' user='...'")
cursor=connection.cursor()
cursor.execute('''SELECT '%' ''') # Does not fail
cursor.execute('''SELECT '%' ''', ()) # Does fail

Traceback (most recent call last):
File "/localhome/modw/tmp/t.py", line 5, in <module>
cursor.execute('''SELECT '%' ''', ()) # Does fail
IndexError: tuple index out of range

Is this a bug in psycopg2?

How do other PEP 249 implementation behave?

Regards,
Thomas

--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Aug 1 '08 #1
5 2345
Thomas Guettler schrieb:
Hi,

I discovered this:

import psycopg2
connection=psycopg2.connect("dbname='...' user='...'")
cursor=connection.cursor()
cursor.execute('''SELECT '%' ''') # Does not fail
cursor.execute('''SELECT '%' ''', ()) # Does fail

Traceback (most recent call last):
File "/localhome/modw/tmp/t.py", line 5, in <module>
cursor.execute('''SELECT '%' ''', ()) # Does fail
IndexError: tuple index out of range

Is this a bug in psycopg2?

How do other PEP 249 implementation behave?
Not a bug. The second execute is the parametrized variant, which simply
tries to fetch the parameters from the passed collection. As you give an
empty collection, but specify one parameter, the error is quite obvious.

The first execute takes the SQL-string as literal.

Diez
Aug 1 '08 #2
Diez B. Roggisch schrieb:
Thomas Guettler schrieb:
>Hi,

I discovered this:

import psycopg2
connection=psycopg2.connect("dbname='...' user='...'")
cursor=connection.cursor()
cursor.execute('''SELECT '%' ''') # Does not fail
cursor.execute('''SELECT '%' ''', ()) # Does fail

Traceback (most recent call last):
File "/localhome/modw/tmp/t.py", line 5, in <module>
cursor.execute('''SELECT '%' ''', ()) # Does fail
IndexError: tuple index out of range

Is this a bug in psycopg2?

How do other PEP 249 implementation behave?

Not a bug. The second execute is the parametrized variant, which simply
tries to fetch the parameters from the passed collection. As you give an
empty collection, but specify one parameter, the error is quite obvious.

The first execute takes the SQL-string as literal.
On the second look, it appears that you don't acutally give a valid
parameter specifier, just a fragment. However, in the same way

"%" % "foo"

yields ValueError - "incomplete format", this is bound to fail - you
need to escape the % with %%.

Diez
Aug 1 '08 #3
On 2008-08-01 15:44, Thomas Guettler wrote:
Hi,

I discovered this:

import psycopg2
connection=psycopg2.connect("dbname='...' user='...'")
cursor=connection.cursor()
cursor.execute('''SELECT '%' ''') # Does not fail
cursor.execute('''SELECT '%' ''', ()) # Does fail

Traceback (most recent call last):
File "/localhome/modw/tmp/t.py", line 5, in <module>
cursor.execute('''SELECT '%' ''', ()) # Does fail
IndexError: tuple index out of range

Is this a bug in psycopg2?

How do other PEP 249 implementation behave?
This depends a lot on the database backend. Some might
complain about the use of an incomplete binding parameter
marker '%' and issue a ProgrammingError even if you don't
pass in any binding parameters (to inform you of the possible
bug in your application).

However, "'%'" may also be perfectly valid SQL when used without
binding parameters, so it's not clear whether this case should
always raise an exception.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Aug 01 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
__________________________________________________ ______________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Aug 1 '08 #4
I forgot to mention where I stumbled about this.

Django has a wrapper:
http://code.djangoproject.com/browse...ckends/util.py

def execute(self, sql, params=()):
start = time()
try:
return self.cursor.execute(sql, params)
finally:

Most people don't have a percent sign in the variable sql.

I guess
cursor.execute(sql, None)
is not portable for all database backends.

I guess this should be the best solution:
if params:
return self.cursor.execute(sql, params)
else:
return self.cursor.execute(sql)

What do you think?

Thomas

Thomas Guettler schrieb:
Hi,

I discovered this:

import psycopg2
connection=psycopg2.connect("dbname='...' user='...'")
cursor=connection.cursor()
cursor.execute('''SELECT '%' ''') # Does not fail
cursor.execute('''SELECT '%' ''', ()) # Does fail

Traceback (most recent call last):
File "/localhome/modw/tmp/t.py", line 5, in <module>
cursor.execute('''SELECT '%' ''', ()) # Does fail
IndexError: tuple index out of range

Is this a bug in psycopg2?

How do other PEP 249 implementation behave?

Regards,
Thomas
Aug 1 '08 #5
On 2008-08-01 20:38, Thomas Guettler wrote:
I forgot to mention where I stumbled about this.

Django has a wrapper:
http://code.djangoproject.com/browse...ckends/util.py

def execute(self, sql, params=()):
start = time()
try:
return self.cursor.execute(sql, params)
finally:

Most people don't have a percent sign in the variable sql.

I guess
cursor.execute(sql, None)
is not portable for all database backends.

I guess this should be the best solution:
if params:
return self.cursor.execute(sql, params)
else:
return self.cursor.execute(sql)

What do you think?
Not good enough... you should use this:

def execute(self, sql, params=None):
start = time()
try:
if params is None:
return self.cursor.execute(sql)
else:
return self.cursor.execute(sql, params)
finally:
...
Thomas

Thomas Guettler schrieb:
>Hi,

I discovered this:

import psycopg2
connection=psycopg2.connect("dbname='...' user='...'")
cursor=connection.cursor()
cursor.execute('''SELECT '%' ''') # Does not fail
cursor.execute('''SELECT '%' ''', ()) # Does fail

Traceback (most recent call last):
File "/localhome/modw/tmp/t.py", line 5, in <module>
cursor.execute('''SELECT '%' ''', ()) # Does fail
IndexError: tuple index out of range

Is this a bug in psycopg2?

How do other PEP 249 implementation behave?

Regards,
Thomas
--
http://mail.python.org/mailman/listinfo/python-list
--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Aug 01 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
__________________________________________________ ______________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Aug 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sven Jacobs | last post by:
Dear newsgroup, I've upgraded to PEAR::DB 1.6.5 recently. Unfortunately now the database connection doesn't work as expected anymore :( The...
2
by: Oliver Peek | last post by:
Hi all, Probably my fault, I'm opening a Berkeley DB file, iterating through and deleting keys, well making an attempt. I get this error when I...
7
by: martijn | last post by:
WHen I use the code below and printing all the results i get this: ------ 0 1 10 11 2 3 4 5 6 7 8 9 ------ But I want ------ 0 1 2
5
by: lwoods | last post by:
When I execute the following script I can connect using 'mysqli_connect' but I get a "DB Error: Not Found" when I display the $db->Message() after...
5
by: Ale | last post by:
Ciao a tutti. Ho eliminato il database di esempio SAMPLE(non cancellato). Ora lo voglio reinstallare ma durante l'installazione mi da il seguente...
22
by: Smutny30 | last post by:
Hello, I am preparing a database that will store 10 n * GBs - 100 n * GBs of data. I calculated to have 1,2 GB of bufferpools. I run the DB2 v....
2
by: Greg Strong | last post by:
Hello All, I've written code in a test database with test data. Everything seems to be working except compact database in VB code per...
2
by: Sam Shaw | last post by:
I have been looking after an MS Access database, using table links to access data in a back-end MDB database. We have recently micrated to a SQL...
2
by: daralthus | last post by:
Hello! I would like to ask your help, i have founded a great code here:...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.