Android,处理 SQLiteConstraintException

发布于 2024-12-14 04:05:48 字数 518 浏览 1 评论 0原文

我在 Android 中有这个方法:

public void insertarTitulo(String _id, String title, String url){
    ContentValues cv = new ContentValues();

    cv.put("_id", _id);
    cv.put("title", title);
    cv.put("URL", url);

try{
        getWritableDatabase().insert("book", "book", cv);
}catch(SQLiteConstraintException e){

       Log.e(MyActivity.LOGTAG,   "This code doesn't show");


}
    close();

}

title 值是唯一的,因此当我插入重复值时会抛出约束错误,到目前为止这是正确的。 一切都按预期进行。 但我无法使 catch 代码起作用。

I've got this method in Android:

public void insertarTitulo(String _id, String title, String url){
    ContentValues cv = new ContentValues();

    cv.put("_id", _id);
    cv.put("title", title);
    cv.put("URL", url);

try{
        getWritableDatabase().insert("book", "book", cv);
}catch(SQLiteConstraintException e){

       Log.e(MyActivity.LOGTAG,   "This code doesn't show");


}
    close();

}

title value is unique so when I insert a duplicate value throws me the Constraint error, so far that's correct.
Everything works as expected.
but I can't make the catch code work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

起风了 2024-12-21 04:05:48

尝试使用 插入或抛出。否则,如果发生错误,insert 仅返回 -1。

Try using insertOrThrow. Otherwise, insert just returns -1 in the event of an error.

三人与歌 2024-12-21 04:05:48

当执行从入口类添加数据库中的值时添加boolean来控制数据
例如:

   boolean DBAccepted = DBhelper.addData(blabla);

if(DBAccepted ==false)
{
Toast.maketext (this,"There is a conflict",Toast.LengthLong ).show();
}

在 DBhelper 类中:

public boolean addData(String blablabla){
    DBAccepted =true;
ContentValues cv = new ContentValues();
    cv.put(key_value, blablabla);
     Databes.insertOrThrow(TABLE_SCORES, null, cv);
    } catch (SQLiteConstraintException e) {
                // TODO Auto-generated catch block
                    System.out.println(e);
                    DBAccepted =false;
            }
        return DBAccepted ;
    }

如果传递的值没有冲突或错误,则这里的数据库将尝试添加值,如果发生错误,布尔值将保持为 true,则布尔值将为 false 并将返回它,并且您可以将任何消息作为 ex toast (有冲突)

When you execute to add the values in the database from the entry class add boolean to control the data
ex:

   boolean DBAccepted = DBhelper.addData(blabla);

if(DBAccepted ==false)
{
Toast.maketext (this,"There is a conflict",Toast.LengthLong ).show();
}

in the DBhelper Class :

public boolean addData(String blablabla){
    DBAccepted =true;
ContentValues cv = new ContentValues();
    cv.put(key_value, blablabla);
     Databes.insertOrThrow(TABLE_SCORES, null, cv);
    } catch (SQLiteConstraintException e) {
                // TODO Auto-generated catch block
                    System.out.println(e);
                    DBAccepted =false;
            }
        return DBAccepted ;
    }

The database here will try to add the values if the values passed without conflict or error the boolean will remain true if error the boolean will be false and will return it and you can toast any message as ex toast (there is a conflict)

月棠 2024-12-21 04:05:48

抱歉,上面提到的答案在 android 4.4.2 上不适用于我。
最小 sdk 级别 11,最大 21。

对我有用的是以下内容:

/*FIRST INSERT A VALUE*/
database = //get a writable database instance
String MESSAGES_TABLE = "messages";
ContentValues cv= new ContentValues();
cv.put("unique_id_column","unique_id");
database.insert(MESSAGES_TABLE,null,cv);
database.close();



/*NOW LETS TRY TO INSERT ANOTHER RECORD, WITH THE SAME VALUE, WHERE UNIQUE_ID_COLUMN
IS THE UNIQUE COLUMN DEFINED WHILE CREATING THE TABLE.*/
database == //get a writable database instance
try{            
    long new_row_id = database.insertWithOnConflict(MESSAGES_TABLE, null,cv,SQLiteDatabase.CONFLICT_FAIL);
    Log.d(SS_DATABASE_TAG,"the new row id is ---> " + String.valueOf(new_row_id));          
    }
catch(SQLiteConstraintException e)
    {
    Log.d(SS_DATABASE_TAG,"caught the contraint.");
    }
database.close();

这正确捕获了约束,并且您可以在 catch 块内进行处理。

RR。

Sorry but the answers mentioned above do not work for me on android 4.4.2.
min sdk level 11, max 21.

What worked for me was the following:

/*FIRST INSERT A VALUE*/
database = //get a writable database instance
String MESSAGES_TABLE = "messages";
ContentValues cv= new ContentValues();
cv.put("unique_id_column","unique_id");
database.insert(MESSAGES_TABLE,null,cv);
database.close();



/*NOW LETS TRY TO INSERT ANOTHER RECORD, WITH THE SAME VALUE, WHERE UNIQUE_ID_COLUMN
IS THE UNIQUE COLUMN DEFINED WHILE CREATING THE TABLE.*/
database == //get a writable database instance
try{            
    long new_row_id = database.insertWithOnConflict(MESSAGES_TABLE, null,cv,SQLiteDatabase.CONFLICT_FAIL);
    Log.d(SS_DATABASE_TAG,"the new row id is ---> " + String.valueOf(new_row_id));          
    }
catch(SQLiteConstraintException e)
    {
    Log.d(SS_DATABASE_TAG,"caught the contraint.");
    }
database.close();

this correctly catches the constraint, and you can do your handling inside the catch block.

RR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文