Android,处理SQLiteConstraintException [英] Android, handle SQLiteConstraintException

查看:102
本文介绍了Android,处理SQLiteConstraintException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有此方法:

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 值是唯一的,因此当我插入重复值时,抛出约束错误,到目前为止,这是正确的。
一切正常。
,但我无法使捕获代码起作用。

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.

推荐答案

尝试使用 insertOrThrow 。否则,如果发生错误,insert只会返回-1。

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

这篇关于Android,处理SQLiteConstraintException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆