【问题标题】:SQLException error handling in androidandroid中的SQLException错误处理
【发布时间】:2014-03-04 09:14:20
【问题描述】:

嗨,在我的项目中,我需要处理重复值、空字段等异常。我想为此使用 try catch 方法

public void deleteRegisterID(Object object) {
    SQLiteDatabase oDBLocal = this.getWritableDatabase();
    try {
        oDBLocal.execSQL("delete from " + STRTABLE_REGISTER + " where " + KEY_ID
                + " = " + object + ";");
    }catch (SQLException mSQLException) {
        Log.e("Loginerror", "getproductData >>" + mSQLException.toString());
        throw mSQLException;
    }
}

我能够得到错误,但我需要处理它并向用户发送一些消息以再次输入,所以我想在this 的帮助下这样做

catch (SQLException mSQLException)
    {
       switch (mSQLException.getErrorCode ())
       {
          case 19:
             //some toast message to user.
             break;
          case 11:
             //some toast message to user.
             break;
          default:
             throw mSQLException;
       }
     }

但是当我检查mSQLException.时,SQLException中没有getErrorCode()这样的方法,但它在android文档中有任何人可以帮助我

【问题讨论】:

    标签: android database sqlite exception sqlexception


    【解决方案1】:

    首先我想解释一下 SQLException 类。在 Android 中,您可以访问 2 个 SQLException 类,android.database.SQLExceptionjava.sql.SQLException。您的链接有关于 java.sql.SQLException class 的解释。请检查this 链接,这将包含有关 android.database.SQLException 类的信息您可能使用了android.database.SQLException 类。我们在这个类中没有getErrorCode() 方法。

    我可以为您的问题提供临时解决方案。您可以使用以下实现来解决您的问题。

    catch (SQLException mSQLException) {
                if(mSQLException instanceof SQLiteConstraintException){
                    //some toast message to user.
                }else if(mSQLException instanceof SQLiteDatatypeMismatchException) {
                    //some toast message to user.
                }else{
                    throw mSQLException;
                }
            }
    

    您可以从这个link的子类列表中选择任何子类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2011-02-17
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多