Cannot fetch from database using if...else

Discussion in 'Ruby' started by Arun Kumar, Mar 14, 2009.

  1. Arun Kumar

    Arun Kumar Guest

    Hi,
    I'm new to ruby and right now i'm trying out some examples involving
    database access using the dbi module. This is my code...

    #!/usr/local/bin/ruby -w

    require 'dbi'

    begin
    con = DBI.connect("DBI:Mysql:Sample:localhost", "arunkumar", "123456")
    stats = con.prepare("Select * from hello where first_name = ?")
    stats.execute('arun')
    if stats.fetch == nil
    puts "No Records"
    con.rollback
    else
    puts stats.fetch
    end
    rescue DBI::DatabaseError => e
    puts "Error: #{e.errstr}"
    ensure
    con.disconnect if con
    end

    If I use 'puts stats.fetch' before if the fetched data is displayed
    properly. But if i use 'puts stats.fetch' inside if or else 'nil' will
    be displayed even if there are matching records in the database. I dont
    know why. Can anybody help me?????

    Regards
    Arun Kumar
    --
    Posted via http://www.ruby-forum.com/.
    Arun Kumar, Mar 14, 2009
    #1
    1. Advertising

  2. From the API doc:
    fetch():
    Fetch the next row in the result set. DBD Required.

    So someone will correct me if I'm wrong but I think you cannot evaluate
    fetch in a condition and call it in the condition itself because it will
    refer to the next row in the result set, probably nil in your case
    because there is only one row.

    Luc

    Arun Kumar a écrit :
    > Hi,
    > I'm new to ruby and right now i'm trying out some examples involving
    > database access using the dbi module. This is my code...
    >
    > #!/usr/local/bin/ruby -w
    >
    > require 'dbi'
    >
    > begin
    > con = DBI.connect("DBI:Mysql:Sample:localhost", "arunkumar", "123456")
    > stats = con.prepare("Select * from hello where first_name = ?")
    > stats.execute('arun')
    > if stats.fetch == nil
    > puts "No Records"
    > con.rollback
    > else
    > puts stats.fetch
    > end
    > rescue DBI::DatabaseError => e
    > puts "Error: #{e.errstr}"
    > ensure
    > con.disconnect if con
    > end
    >
    > If I use 'puts stats.fetch' before if the fetched data is displayed
    > properly. But if i use 'puts stats.fetch' inside if or else 'nil' will
    > be displayed even if there are matching records in the database. I dont
    > know why. Can anybody help me?????
    >
    > Regards
    > Arun Kumar
    >
    Luc Traonmilin, Mar 14, 2009
    #2
    1. Advertising

  3. On Sat, Mar 14, 2009 at 6:29 AM, Arun Kumar
    <> wrote:
    > Hi,
    > I'm new to ruby and right now i'm trying out some examples involving
    > database access using the dbi module. This is my code...
    >
    > #!/usr/local/bin/ruby -w
    >
    > require 'dbi'
    >
    > begin
    > con =3D DBI.connect("DBI:Mysql:Sample:localhost", "arunkumar", "123456")
    > stats =3D con.prepare("Select * from hello where first_name =3D ?")
    > stats.execute('arun')
    > if stats.fetch =3D=3D nil
    > =C2=A0 =C2=A0puts "No Records"
    > =C2=A0 =C2=A0con.rollback
    > else
    > =C2=A0 =C2=A0puts stats.fetch
    > end
    > rescue DBI::DatabaseError =3D> e
    > =C2=A0 =C2=A0puts "Error: #{e.errstr}"
    > ensure
    > =C2=A0 =C2=A0con.disconnect if con
    > end
    >
    > If I use 'puts stats.fetch' before if the fetched data is displayed
    > properly. But if i use 'puts stats.fetch' inside if or else 'nil' will
    > be displayed even if there are matching records in the database. I dont
    > know why. Can anybody help me?????



    Since stats.fetch is a method call, and you want to output the same
    result you are testing, not call the method again and output whatever
    it would return the second time if it is not nil the first time, you
    probably want to replace the if...else...end structure with something
    like this:

    if (result =3D stats.fetch) =3D=3D nil
    puts "No Records"
    con.rollback
    else
    puts result
    end

    Since "nil" is the only result that is false in a boolean context that
    fetch will return, you don't really need a comparison to nil:

    if (result =3D stats.fetch)
    puts "No Records"
    con.rollback
    else
    puts result
    end
    Christopher Dicely, Mar 14, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vikas khengare
    Replies:
    9
    Views:
    8,047
    jcsnippets.atspace.com
    Feb 9, 2006
  2. Replies:
    25
    Views:
    661
    MonkeeSage
    Oct 1, 2006
  3. kj
    Replies:
    15
    Views:
    552
    Lawrence D'Oliveiro
    May 23, 2009
  4. A
    Replies:
    8
    Views:
    799
    James Kanze
    Aug 28, 2010
  5. Navneet Mathpal
    Replies:
    0
    Views:
    150
    Navneet Mathpal
    Feb 28, 2014
Loading...

Share This Page