美文网首页
数据库插入数据失败的解决办法-主键(primary key) 的

数据库插入数据失败的解决办法-主键(primary key) 的

作者: fc82bb084ee7 | 来源:发表于2017-12-04 16:31 被阅读167次
    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed
    

    存在2条预警信息时, 调用addToAlarms()向数据库插入数据, 但第二次插入数据时, 报exception, 插入数据失败.

        public void addToAlarms(String area_weather_id, WeatherAlarm alarm) {
            mDB.beginTransaction();
            try {
                ContentValues cv = new ContentValues();
                cv.put(WeatherDBOpenHelper.AREA_WEATHER_ID, area_weather_id);
                cv.put(WeatherDBOpenHelper.ALARMS_AREA_NAME, alarm.getAreaName());
                cv.put(WeatherDBOpenHelper.ALARMS_AREA_LAST_ACTION, alarm.getAreaLastAction());
                cv.put(WeatherDBOpenHelper.ALARMS_AREA_TEXT, alarm.getAreaText());
                cv.put(WeatherDBOpenHelper.ALARMS_BEGIN_TIME, alarm.getBeginTime());
                cv.put(WeatherDBOpenHelper.ALARMS_END_TIME, alarm.getEndTime());
                cv.put(WeatherDBOpenHelper.ALARMS_COLOR, alarm.getColor());
                cv.put(WeatherDBOpenHelper.ALARMS_DESCRIPTION, alarm.getDescription());
                cv.put(WeatherDBOpenHelper.ALARMS_SOURCE, alarm.getSource());
                cv.put(WeatherDBOpenHelper.ALARMS_CATEGORY, alarm.getCategory());
                cv.put(WeatherDBOpenHelper.ALARMS_TYPE, alarm.getType());
                cv.put(WeatherDBOpenHelper.ALARMS_INDEX, alarm.getTypeIndex());
                cv.put(WeatherDBOpenHelper.ALARMS_MOBILELINK, alarm.getMobileLink());
    
                mDB.insert(WeatherDBOpenHelper.TABLE_ALARMS, null, cv);
                mDB.setTransactionSuccessful();
            } finally {
                mDB.endTransaction();
            }
        }
    

    第一次插入正确,当第二次插入时相当于再次插入同样的信息,由于id已存在,所以抛出了异常.
    原因是, 创建table时, 把AREA_WEATHER_ID作为主键声明导致的.

            db.execSQL("create table " + TABLE_ALARMS + "(" + AREA_WEATHER_ID + " text primary key , "
                    + ALARMS_AREA_NAME + " text , " + ALARMS_AREA_LAST_ACTION + " text , "
                    + ALARMS_AREA_TEXT + " text , " + ALARMS_BEGIN_TIME + " text , "
                    + ALARMS_END_TIME + " text , " + ALARMS_COLOR + " text , "
                    + ALARMS_DESCRIPTION + " text , " + ALARMS_SOURCE + " text , "
                    + ALARMS_CATEGORY + " text , " + ALARMS_TYPE + " text , "
                    + ALARMS_INDEX + " text , "
                    + ALARMS_MOBILELINK + " text)");
    

    改成这样就解决了.

            db.execSQL("create table " + TABLE_ALARMS + "(" + AREA_WEATHER_ID + " text , "
                    + ALARMS_AREA_NAME + " text , " + ALARMS_AREA_LAST_ACTION + " text , "
                    + ALARMS_AREA_TEXT + " text , " + ALARMS_BEGIN_TIME + " text , "
                    + ALARMS_END_TIME + " text , " + ALARMS_COLOR + " text , "
                    + ALARMS_DESCRIPTION + " text , " + ALARMS_SOURCE + " text , "
                    + ALARMS_CATEGORY + " text , " + ALARMS_TYPE + " text , "
                    + ALARMS_INDEX + " text , "
                    + ALARMS_MOBILELINK + " text)");
    

    refer to:
    http://blog.sina.com.cn/s/blog_6182981401018z9u.html

    相关文章

      网友评论

          本文标题:数据库插入数据失败的解决办法-主键(primary key) 的

          本文链接:https://www.haomeiwen.com/subject/oupxixtx.html