Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQL: Check if Record in Database Exists

Tags:

android

sql

I am trying to query my database based on a specific id.

String sql = "SELECT * FROM mash WHERE recipe_id = '" + id + "'";
Cursor data = database.rawQuery(sql, null);

If this is the first time the activity has been run, the table WILL exist along with the id column, but there will not be a record with the specific id. How can I check if that specific record exists and if it does not, add it? I have found lots of into regarding checking if a specific column exists, but nothing about checking if a specific record exists.

So far I have tried getting the id column index and checking to see if it returns -1, but it actually is returning 1 for some reason. What can I use in an if statement to verify that the id column has not yet been created?

like image 443
ryandlf Avatar asked Jul 30 '11 21:07

ryandlf


1 Answers

if (cursor.moveToFirst()) {
    // record exists
} else {
    // record not found
}
like image 117
aromero Avatar answered Oct 19 '22 09:10

aromero