I want to check if records exist in the SQLite database, here\'s what I have done so far. when I search for a already existing records im getting value from EditText in list.
Select a single row from your table, is you get results = it exists, else it doesn't.
public boolean doesStationExist(String name){
final String query = "SELECT stationName FROM review WHERE stationId='"+name+"' LIMIT 1";
try (SQLiteDatabase database = getReadableDatabase();
Cursor c = database.rawQuery(query, null)) {
return c.moveToFirst();
}
}
By the way, the cursor returned from rawQuery
is never null
.
Edit:
The updated code will close the database and the cursor once the try
block finishes. This is called try-with-resources.