简体   繁体   中英

Check if record exist in the database android

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.

    //code from activity class

    public View.OnClickListener searchStation = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        arrayAdapterList=new ArrayAdapter<String>(SearchAndReview.this,
                android.R.layout.simple_list_item_1,
                list);
        String searchTextFinal=searchText.getText().toString();
        dbms.searchStation(searchTextFinal);
        Cursor cursor= dbms.searchStation(searchTextFinal);



       list.add(searchTextFinal);
        arrayAdapterList= new ArrayAdapter<String>(SearchAndReview.this,
                android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapterList);


    }
};

// code from databasehelper class

   public Cursor searchStation(String name){
    SQLiteDatabase database = this.getWritableDatabase();
    String searchStationQuery = "SELECT stationName FROM review WHERE stationId='"+ name+"'";
    Cursor c =database.rawQuery(searchStationQuery,null);
    if (c != null && c.moveToFirst()) {
    }
    return c;
}

I use this code in my application and it works perfact...

LoginActivity.java

DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());
String email_id = email.getText().toString();
boolean dbHelper.isExist(email_id);

// if record is exist then it will return true otherwise this method returns false


Using rawQuery

 public boolean isExist(String strEmailAdd) {
        db = this.getReadableDatabase();
        cur = db.rawQuery("SELECT * FROM " + USER_TABLE + " WHERE email_id = '" + strEmailAdd + "'", null);
        boolean exist = (cur.getCount() > 0);
        cur.close();
        db.close();
        return exist;

    }

Using db.query

public boolean isExist(String strEmailAdd){

        String whereClause = "email_id = ?";
        String[] whereArgs = new String[]{strEmailAdd};

        db = database.getWritableDatabase();
        cur = db.query(USER_TABLE, null, whereClause, whereArgs, null, null, null);
        boolean exist = (cur.getCount() > 0);
        cur.close();
        db.close();
        return exist;
}

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.

DatabaseManager.java

public Cursor checkFavourites()
{

    String query="select * from "+DATABASE_TABLE;
    Cursor cursor=db.rawQuery(query,null);

    return cursor;
}

MainActivity.java

public void favourite_Click(View view){
    Cursor cursor=dbManager.checkFavourites();
    if (cursor.getCount()>0)
    {
        Toast.makeText(MainActivity.this, "Favourites", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(MainActivity.this,"Nothing in favourites yet", Toast.LENGTH_SHORT).show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM