我通过在EditText中输入密钥来访问SQLiteDatabase中的数据。每当我输入不在数据库中的错误键时,我的应用程序就会停止。我不希望我的应用程序停止,相反,我想在这里显示为吐司。
我该怎么做呢?
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;
} 发布于 2013-07-15 18:12:00
在使用游标之前始终检查游标计数,如下所示
if (cursor != null && cursor.getCount() > 0) {
// your logic goes here
} else {
Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
}发布于 2013-07-15 18:08:06
我认为您应该使用c == null而不是c.equals(null),因为
来自文档:
equals方法在非空对象引用上实现等价关系
..。
对于任何非空的引用值x和y,当且仅当x和y引用同一对象时,此方法才返回true (x == y的值为true)。
https://stackoverflow.com/questions/17651486
复制相似问题