0

SQLiteDatabase通过在 中输入密钥来访问数据EditText。每当我输入一些错误的密钥(不在数据库中)时,我的应用程序就会停止。我不想让我的应用程序停止,而是想在这里显示为 toast。
我怎样才能做到这一点?

  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; 
    } 
4

2 回答 2

1

在使用光标之前始终检查光标计数

            if (cursor != null && cursor.getCount() > 0) {
                // your logic goes here
            } else {
                Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
            }
于 2013-07-15T10:11:59.713 回答
0

我相信你应该使用c == null而不是c.equals(null)因为

来自文档:

equals 方法在非空对象引用上实现等价关系

...

对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象(x == y 的值为 true)时,此方法才返回 true。

于 2013-07-15T10:08:06.247 回答