Reputation: 75
i trying to search in database whether the id is exist or not, when it exist i open the next activity , but when it not exist the app crash.. i coundnt find the bug , error Cursor Index out of bound
Database dbc = new Database(this);
dbc.open();
String id = searchId.getText().toString();
boolean checkId = dbc.isGotId(id);
if(checkId == true){
String s = searchId.getText().toString();
Bundle b = new Bundle();
b.putString("key",s);
b.putInt("keyX", radioBtnFlag);
Intent a = new Intent(SearchUpdate.this, UpdateDelete.class);
a.putExtras(b);
startActivity(a);
searchId.setText(null);
}else if(checkId == false){
Log.v(id, id + "2222222");
Dialog d = new Dialog(this);
d.setTitle("Error!");
TextView tv = new TextView(this);
tv.setText("This Search is allow only ID! "+ radioBtnFlag);
d.setContentView(tv);
d.show();
searchId.setText(null);
and here ...
public boolean isGotId(String id){
boolean result = false;
try{
Cursor sId = ourDatabase.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + Pro_ID + "=" + id, null);
result = true;
}catch(SQLiteException e)
{
result = false;
}//catch
return result;
}//isGOtId
Upvotes: 2
Views: 149
Reputation: 4571
Try this...
public boolean isGotId(String id){
Cursor sId = ourDatabase.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
Pro_ID + "=" + id, null);
int numberOfRows = sId.getCount();
if(numberOfRows <= 0)
{
return false;
}
return true;
}
Upvotes: 4
Reputation: 12753
try{
Cursor sId = ourDatabase.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + Pro_ID + "=" + id, null);
if(sId.moveToFirst() && sId ! = null)
result = true;
}catch(SQLiteException e)
{
result = false;
}
Upvotes: 3