约束违反Sqlite3 Android

时间:2014-04-15 05:40:58

标签: android sqlite constraints android-sqlite

  String CREATE_TRAIN_DATABASE = "CREATE TABLE IF NOT EXISTS "
        + DATABASE_TRAIN_FEATURES
        + "("
        + KEY_ROW_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + KEY_USER_ID
        + " TEXT NOT NULL,"
        + KEY_COL_MEANX
        + " REAL NOT NULL,"
        + KEY_COL_MEANY
        + " REAL NOT NULL,"
        + KEY_COL_MEANZ
        + " REAL NOT NULL,"
        + KEY_COL_VARIANCEX
        + " REAL NOT NULL,"
        + KEY_COL_VARIANCEY
        + " REAL NOT NULL,"
        + KEY_COL_VARIANCEZ
        + " REAL NOT NULL,"
        + KEY_COL_CORRX
        + " REAL NOT NULL,"
        + KEY_COL_CORRY
        + " REAL NOT NULL,"
        + KEY_COL_CORRZ
        + " REAL NOT NULL,"
        + DATABASE_COL_Activity + " TEXT NOT NULL);";

在其他地方我添加数据:

  ContentValues values = new ContentValues();
  String [] cols = setupCols(table);
  values.put(cols[0], email);
    int i = 1;
    for (Float reading : features) {
        values.put(cols[i], reading);
        ++i;
    }

  private String[] setupCols(String table) {
    if (DatabaseHelper.DATABASE_RAW_DATA.compareTo(table) != 0) {
        String[] columns = { DatabaseHelper.KEY_USER_ID,
                DatabaseHelper.KEY_COL_MEANX, DatabaseHelper.KEY_COL_MEANY,
                DatabaseHelper.KEY_COL_MEANZ,
                DatabaseHelper.KEY_COL_VARIANCEX,
                DatabaseHelper.KEY_COL_VARIANCEY,
                DatabaseHelper.KEY_COL_VARIANCEZ,
                DatabaseHelper.KEY_COL_CORRX, DatabaseHelper.KEY_COL_CORRY,
                DatabaseHelper.KEY_COL_CORRZ,
                DatabaseHelper.DATABASE_COL_Activity };
        return columns;
    } 
}

就数据而言,一切看起来都不错。我没有看到任何空值,数据本身看起来对我的应用程序来说是合理的。但我不断遇到约束违规错误。我无法弄清楚为什么。它可能是自动增量部分吗?也就是说,如果我写了一些数据,然后尝试重写一些数据(比如,在我重新启动应用程序之后),它会尝试使用已经使用过的主键重新添加数据。例如,如果我添加一些主键为1的数据,然后重新启动应用程序,并尝试重新添加数据,它会在1处重新开始吗?

错误讯息:

  Error inserting Activity=Sitting VARIANCEY=0.0 CORRZ=0.0 VARIANCEZ=0.0 CORRY=0.0   
  user_id=admin CORRX=0.0 VARIANCEX=.233333332 MEANX = 617.7 MEANY=523.0 MEANZ=527.0
  android.database.sqlite.SQLLiteConstraintException:error code 19:constraint failed

1 个答案:

答案 0 :(得分:0)

在您的错误消息中,我没有看到CORRX列被定义为非空列。