473,573 Members | 3,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting an empty tuple

Hey there,
i have a simple database query that returns as a tuple the number of
rows that the query selected.
kinda like this
cursor.execute( 'select value from table where autoinc > 234')
x = cursor.fetchall ()
print x 21L
ok, means 21 rows met the criteria of the query. but if there are none
that match,
like i do a
print x
0L


how do i encorporate that into an equation ?
i have tried all kinds of stuff

if x == 0L
if x(0) == None
if x == None

anyway, what shoud i do to test if the result is empty?

thanks

Jul 31 '05 #1
3 4864
On 31 Jul 2005 08:40:26 -0700, ne*****@xit.net <ne*****@xit.ne t> wrote:
how do i encorporate that into an equation ?
i have tried all kinds of stuff

if x == 0L
if x(0) == None
if x == None

anyway, what shoud i do to test if the result is empty?


Just like any other test:

if not x:

--

# p.d.
Jul 31 '05 #2
On Sun, 31 Jul 2005 08:40:26 -0700, nephish wrote:
Hey there,
i have a simple database query that returns as a tuple the number of
rows that the query selected.
kinda like this
cursor.execute( 'select value from table where autoinc > 234')
x = cursor.fetchall ()
print x 21L

21L is not a tuple, it is a long integer.
ok, means 21 rows met the criteria of the query. but if there are none
that match,
like i do a
print x
0L

how do i encorporate that into an equation ?
i have tried all kinds of stuff


And did they work? If they didn't work, tell us the exact error message
you got.

if x == 0L
If x is a long integer, then that will work. Of just "if x == 0:" will
work too.

if x(0) == None
No. That means x is a function, and you are giving it an argument of 0,
and it returns None.
if x == None
You said that your query returns a tuple, but then gave an example where
it returns a long int. None is not a tuple, nor a long int, so testing
either of those things against None will never be true.

Did you try any of these things in the interactive interpreter? Python is
a great language for experimenting, because you can try this yourself:

# run your setup code ...
# and then do some experimenting
cursor.execute( 'select value from table where autoinc > 99999999')
# or some value that will never happen
x = cursor.fetchall ()
print x

What do you get?

anyway, what shoud i do to test if the result is empty?


Long ints are never empty, but they can be zero:

if x == 0:
print "no rows were found"
elif x == 1:
print "1 row was found"
else:
print "%d rows were found" % x

Tuples can be empty:

if len(x) == 0:
print "no rows were found"

or if you prefer:

if x == ():
print "no rows were found"
But the cleanest, most Pythonic way is just to do a truth-test:

if x:
print "something was found"
else:
print "x is empty, false, blank, nothing..."
--
Steven.

Jul 31 '05 #3
ok, this is what works:
if x == ():

sorry about the bad info. and what i ment to put was
x[0] not x(0)

thanks for the tips
its all good now

shawn
Steven D'Aprano wrote:
On Sun, 31 Jul 2005 08:40:26 -0700, nephish wrote:
Hey there,
i have a simple database query that returns as a tuple the number of
rows that the query selected.
kinda like this
> cursor.execute( 'select value from table where autoinc > 234')
> x = cursor.fetchall ()
> print x

> 21L


21L is not a tuple, it is a long integer.
ok, means 21 rows met the criteria of the query. but if there are none
that match,
like i do a
> print x
> 0L


how do i encorporate that into an equation ?
i have tried all kinds of stuff


And did they work? If they didn't work, tell us the exact error message
you got.

if x == 0L


If x is a long integer, then that will work. Of just "if x == 0:" will
work too.

if x(0) == None


No. That means x is a function, and you are giving it an argument of 0,
and it returns None.
if x == None


You said that your query returns a tuple, but then gave an example where
it returns a long int. None is not a tuple, nor a long int, so testing
either of those things against None will never be true.

Did you try any of these things in the interactive interpreter? Python is
a great language for experimenting, because you can try this yourself:

# run your setup code ...
# and then do some experimenting
cursor.execute( 'select value from table where autoinc > 99999999')
# or some value that will never happen
x = cursor.fetchall ()
print x

What do you get?

anyway, what shoud i do to test if the result is empty?


Long ints are never empty, but they can be zero:

if x == 0:
print "no rows were found"
elif x == 1:
print "1 row was found"
else:
print "%d rows were found" % x

Tuples can be empty:

if len(x) == 0:
print "no rows were found"

or if you prefer:

if x == ():
print "no rows were found"
But the cleanest, most Pythonic way is just to do a truth-test:

if x:
print "something was found"
else:
print "x is empty, false, blank, nothing..."
--
Steven.


Jul 31 '05 #4

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

Similar topics

23
40605
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there...
3
1632
by: Dave Opstad | last post by:
I fully understand why this happens: ---------------------------- >>> a = , , , , ] >>> b = ] * 5 >>> a , , , , ] >>> b , , , , ] >>> a == b
1
3242
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can pickle it? Thanks , Fedor
4
1871
by: s99999999s2003 | last post by:
hi the database "execute" function returns a list of logical results. Each logical result is a list of row tuples, as explained in the documents. everytime i use it to execute various statements, it returns me, for example (, , ) and sometimes , (, ) or () in my call, i give eg (a,b,c) = db.execute(stmt) so that it returns me (, ,
2
3479
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These questions are stimulated by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 Looking at that, what fails if I leave out the following line? ::
9
10049
by: Plissken.s | last post by:
Hi, how can i compare a string which is non null and empty? i look thru the string methods here, but cant find one which does it? http://docs.python.org/lib/string-methods.html#string-methods In java,I do this:
0
2619
by: rautsmita | last post by:
hello friends , i am using to jdk6 and JAXB2.0, i have geomtry.xsd file i am trying to compile this file using jaxb but i got some error i.e.The particle of the type is not a valid restriction of the particle of the base this xsd file is valid as per w3c standard i am also providing error in detail and geometry.xsd file geometry.xsd <?xml...
20
24692
by: Sun | last post by:
Maybe this is a very primative question, but I just get a bit confused about 'set' and 'Set' module in python. I understand 'set' is a build in type in python after 2.4(or 2.3) and Set a seperate module, anyhow, I gonna use build in 'set'. then the question is how can I declare a empty set variable as a 'var= ' do to a list variable?
4
2417
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like uniform_tuple <3, doublet; ? Alex Vinokur
0
7686
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6411
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5584
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5289
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2197
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 we have to send another system
1
1297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.