473,465 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2413
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 problems seems to be the method disconnect(), which...
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 try to delete. ---error Traceback (most...
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 attempting the DB::Connect. And, "yes", the...
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 errore: nella finestra con scritto SQL1005N c'č...
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. 8.2.1 alone on 4 GB box. I obtain : ...
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 http://www.mvps.org/access/general/gen0041.htm. The reason I...
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 Server 2000 back-end atabase, once again accessing...
2
by: daralthus | last post by:
Hello! I would like to ask your help, i have founded a great code here: http://www.jamescaws.co.uk/2008/07/dynamically-count-exit-link-clicks-throughs-using-javascript-php/ but it uses Pear DB...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.