Android,处理SQLiteConstraintException

but*_*elo 11 sqlite android exception constraints handle

我在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();

}
Run Code Online (Sandbox Code Playgroud)

标题值是唯一的,所以当我插入重复值时会抛出Constraint错误,到目前为止这是正确的.一切都按预期工作.但我不能使捕获代码工作.

Chr*_*ris 25

尝试使用insertOrThrow.否则,插入只在发生错误时返回-1.