Reputation: 309
I am accessing data from SQLiteDatabase
by entering a key in EditText
. Whenever I enter some wrong key,which is not in the database my application stops. I don't want my application to stop, instead i want to show as toast here.
How can i do this?
try{
Log.e("table", table_name);
mDb = mDbHelper.getReadableDatabase();
Cursor c = mDb.rawQuery("SELECT name,address1,address2,address3,tariff,sup_type,load,load_unit,meter_no,pole_cd,mst FROM "+table_name+" WHERE acc_id = '"+ i +" ' ", null);
if (c.equals(null))
{
c.moveToFirst();
Log.e("c", "i am null"+ c.toString());
}
else if (c!=null) {
c.moveToNext();
Log.e("c", "i am notnull" + c.toString());
}
return c;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
Upvotes: 0
Views: 300
Reputation: 21
Always check Cursor count before using the cursor like
if (cursor != null && cursor.getCount() > 0) {
// your logic goes here
} else {
Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
}
Upvotes: 1
Reputation: 3489
I believe that you should use c == null
instead of c.equals(null)
because
From doc:
The equals method implements an equivalence relation on non-null object references
...
for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Upvotes: 0