android – SQLiteConstraintException不要内部catch

前端之家收集整理的这篇文章主要介绍了android – SQLiteConstraintException不要内部catch前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我运行该代码时,我在db.insert内部有一个异常:
08-29 15:40:17.519: E/sqliteDatabase(3599): android.database.sqlite.sqliteConstraintException: column date is not unique (code 19)

这是正常的,因为我已经把这个价值作为一个关键.
所以在这种情况下,我做了一个sqliteConstraintException的捕获,但是当我运行代码时,我从来没有在catch中.

我究竟做错了什么?

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 });
            }

谢谢.

整个日志:

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)

解决方法

请改用db.insertOrThrow().
原文链接:https://www.f2er.com/android/310477.html

猜你在找的Android相关文章