How to get Result Set has a record?

General discussion on OpenOffice Base and its databases.

How to get Result Set has a record?

Postby Brian L. Chacon » Tue Jun 03, 2014 7:49 am

Hi guys

I want to learn whether I have a record from my ResultSet query
Example code

Code: Select all
ResultSet = Statement.executeQuery("SELECT * FROM mytable WHERE id=1" )
if Not IsNull(ResultSet) Then
    MsgBox("There is a record")
else
    MsgBox("No record in this ID")
end if

But this code does not work! I get always "There is a record" even though I enter different ID number that does not exist in my database
What can i do
Thank you
Brian L. Chacon
 
Posts: 20
Joined: Mon Mar 24, 2014 6:23 am

Re: How to get Result Set has a record?

Postby Keya_LOT » Tue Jun 03, 2014 10:01 am

Hi,
Perhaps, you could try the example code below:
Code: Select all
ResultSet = Statement.executeQuery("SELECT COUNT(*) as TABLE_COUNT FROM mytable WHERE id=1" )

num_of_rows = ResultSet.getINT(1)

if num_of_rows > 0 Then
   MsgBox("There is a record")
else
   MsgBox("No record in this ID")
end if


Explanation: Rather than using the SELECT statement you used, have the database engine return the number of records ( COUNT(*) ) that meet the criteria in the WHERE clause. Then, you can assign the one number returned, to a BASIC variable, in this instance, I used num_of_rows . . . and . . . your if statement can return the correct MsgBox . :)

I hope this helps you
Thanks : :P
Keya_LOT
 
Posts: 24
Joined: Tue Mar 25, 2014 6:35 am


Return to OpenOffice.org Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron