getting an empty tuple

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nephish@xit.net

    #1

    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
    [color=blue][color=green][color=darkred]
    >>> cursor.execute( 'select value from table where autoinc > 234')
    >>> x = cursor.fetchall ()
    >>> print x[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> 21L[/color][/color][/color]

    ok, means 21 rows met the criteria of the query. but if there are none
    that match,
    like i do a
    [color=blue][color=green][color=darkred]
    >>> print x
    >>> 0L[/color][/color][/color]

    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

  • Peter Decker

    #2
    Re: getting an empty tuple

    On 31 Jul 2005 08:40:26 -0700, nephish@xit.net <nephish@xit.ne t> wrote:
    [color=blue]
    > 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?[/color]

    Just like any other test:

    if not x:

    --

    # p.d.

    Comment

    • Steven D'Aprano

      #3
      Re: getting an empty tuple

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

      21L is not a tuple, it is a long integer.
      [color=blue]
      > ok, means 21 rows met the criteria of the query. but if there are none
      > that match,
      > like i do a
      >[color=green][color=darkred]
      >>>> print x
      >>>> 0L[/color][/color]
      >
      > how do i encorporate that into an equation ?
      > i have tried all kinds of stuff[/color]

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

      [color=blue]
      > if x == 0L[/color]

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

      [color=blue]
      > if x(0) == None[/color]

      No. That means x is a function, and you are giving it an argument of 0,
      and it returns None.
      [color=blue]
      > if x == None[/color]

      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?

      [color=blue]
      > anyway, what shoud i do to test if the result is empty?[/color]

      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.

      Comment

      • nephish@xit.net

        #4
        Re: getting an empty tuple

        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:[color=blue]
        > On Sun, 31 Jul 2005 08:40:26 -0700, nephish wrote:
        >[color=green]
        > > Hey there,
        > > i have a simple database query that returns as a tuple the number of
        > > rows that the query selected.
        > > kinda like this
        > >[color=darkred]
        > >>>> cursor.execute( 'select value from table where autoinc > 234')
        > >>>> x = cursor.fetchall ()
        > >>>> print x[/color]
        > >[color=darkred]
        > >>>> 21L[/color][/color]
        >
        > 21L is not a tuple, it is a long integer.
        >[color=green]
        > > ok, means 21 rows met the criteria of the query. but if there are none
        > > that match,
        > > like i do a
        > >[color=darkred]
        > >>>> print x
        > >>>> 0L[/color]
        > >
        > > how do i encorporate that into an equation ?
        > > i have tried all kinds of stuff[/color]
        >
        > And did they work? If they didn't work, tell us the exact error message
        > you got.
        >
        >[color=green]
        > > if x == 0L[/color]
        >
        > If x is a long integer, then that will work. Of just "if x == 0:" will
        > work too.
        >
        >[color=green]
        > > if x(0) == None[/color]
        >
        > No. That means x is a function, and you are giving it an argument of 0,
        > and it returns None.
        >[color=green]
        > > if x == None[/color]
        >
        > 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?
        >
        >[color=green]
        > > anyway, what shoud i do to test if the result is empty?[/color]
        >
        > 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.[/color]

        Comment

        Working...