[DB-SIG] how to compare

Carsten Haese carsten at uniqsys.com
Tue Jan 31 14:28:05 CET 2006


On Tue, 2006-01-31 at 02:53, python eager wrote:
>  Hi ,
> >       here  i am comparing both the values of form value and table 
> value.
> > But it will not work . The result will be print "not equal". Table 
> values
> > are retrieved fine. Here what mistake i did. No compilation error
> and 
> run
> > time error. Please solve this problem.
> >    
> >   Code Snippet :
> >    
> >             req = self.request()
> >           pid = int(req.field('pid'))
> >           self.writeln(pid)
> >          
> >             dbconnection = cx_Oracle.connect("myusername", 
> "mypassword",
> > "mydatabase")
> >           cursor = dbconnection.cursor()
> >           cursor.arraysize = 50
> >           cursor.execute("SELECT PID FROM PERSONALDETAILS order by 
> pid")
> >           for PID in cursor.fetchall():
> >              # Comparing here both formvalue and table value ( both 
> are
> > numeric)              
> >               if pid==PID:
> >                   self.writeln("Equal")
> >               else:
> >                   self.writeln("Not Equal")

First of all, this approach for checking whether a row in a table has a
certain value is very inefficient, to put it mildly. You should really
use a WHERE clause in your query to perform the comparison on the server
side instead of on the client side. (If there is an index on the PID
column, and it sounds like there should be, the server will actually use
an index lookup instead of fetching and inspecting all rows!)

That said, your comparisons are failing because the fetch methods return
tuples, even if you're only selecting a single columns. You're comparing
the integer pid with the singleton tuple PID, which will always be
false. You'd have to compare pid to PID[0] to make the comparison come
out as expected.

Hope this helps,

Carsten.




More information about the DB-SIG mailing list