Check if record exist in the database android

后端 未结 3 434
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 03:53

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.

3条回答
  •  情歌与酒
    2021-01-23 04:27

    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.

提交回复
热议问题