SQLiteConstraintException不进去抓

SQLiteConstraintException不进去抓

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

问题描述

当我运行code我有db.insert内部异常:

  08-29 15:40:17.51​​9:E / SQLiteDatabase(3599):android.database.sqlite.SQLiteConstraintException:列日是不是唯一的(code 19)
 

这是正常的,因为我已经有一个价值的关键。因此,在这种情况下,我做的 SQLiteConstraintException 的追赶,但我从来没有得到过捕里面,当我运行code。

我是什么做错了吗?

 尝试{
                db.insert(TABLE_STATISTICS,空,价值观);
            }赶上(SQLiteConstraintException E){
                Log.d(进入,进入了异常);
                ContentValues​​ values​​Up =新ContentValues​​();
                values​​Up.put((ISMT KEY_MT:KEY_MO),变化);
                db.update(TABLE_STATISTICS,values​​Up,KEY_DATE +=?,
                        新的String [] {m​​essage_date});
            }
 

感谢你。

全日志:

  08-29 15:55:55.870:E / SQLiteDatabase(3828):错误插入MO = 0日期= 2012-08-08 MT = 1
08-29 15:55:55.870:E / SQLiteDatabase(3828):android.database.sqlite.SQLiteConstraintException:列日是不是唯一的(code 19)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(本机方法)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在xx.xxx.xxx.xx.xxxx.StatisticsHolderDay.updateCounterNextStep(StatisticsHolderDay.java:124)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在xx.xxx.xxx.xx.xxxx.StatisticsHolderDay.updateCounter(StatisticsHolderDay.java:102)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在xx.xxx.xxx.xx.xxxx.LastMonthActivity.onStart(LastMonthActivity.java:146)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.Activity.performStart(Activity.java:5018)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.os.Handler.dispatchMessage(Handler.java:99)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.os.Looper.loop(Looper.java:137)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在android.app.ActivityThread.main(ActivityThread.java:4745)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在java.lang.reflect.Method.invokeNative(本机方法)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在java.lang.reflect.Method.invoke(Method.java:511)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-29 15:55:55.870:E / SQLiteDatabase(3828):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

使用 db.insertOrThrow()代替。

When I run that code I have inside the db.insert an exception:

08-29 15:40:17.519: E/SQLiteDatabase(3599): android.database.sqlite.SQLiteConstraintException: column date is not unique (code 19)

Which is normal, because I already have that value as a key.So in that case I do a catch of SQLiteConstraintException but I never get inside the catch when I run the code.

What am I doing wrong?

           try {
                db.insert(TABLE_STATISTICS, null, values);
            } catch (SQLiteConstraintException e) {
                Log.d("entered", "entered the exception");
                ContentValues valuesUp = new ContentValues();
                valuesUp.put((isMT ? KEY_MT : KEY_MO), change);
                db.update(TABLE_STATISTICS, valuesUp, KEY_DATE + "=?",
                        new String[] { message_date });
            }

Thank you.

the whole log:

08-29 15:55:55.870: E/SQLiteDatabase(3828): Error inserting MO=0 date=2012-08-08 MT=1
08-29 15:55:55.870: E/SQLiteDatabase(3828): android.database.sqlite.SQLiteConstraintException: column date is not unique (code 19)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at xx.xxx.xxx.xx.xxxx.StatisticsHolderDay.updateCounterNextStep(StatisticsHolderDay.java:124)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at xx.xxx.xxx.xx.xxxx.StatisticsHolderDay.updateCounter(StatisticsHolderDay.java:102)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at xx.xxx.xxx.xx.xxxx.LastMonthActivity.onStart(LastMonthActivity.java:146)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.Activity.performStart(Activity.java:5018)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.os.Looper.loop(Looper.java:137)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at java.lang.reflect.Method.invokeNative(Native Method)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at java.lang.reflect.Method.invoke(Method.java:511)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-29 15:55:55.870: E/SQLiteDatabase(3828):     at dalvik.system.NativeStart.main(Native Method)
解决方案

Use db.insertOrThrow() instead.

这篇关于SQLiteConstraintException不进去抓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 14:00