SQLiteConstraintException:REAL字段上的NOT NULL约束失败

时间:2017-04-20 18:06:22

标签: android sqlite null android-sqlite nan

我收到了一条通常有用的代码的崩溃报告。

异常在insertOrThrow中启动,并且是:“Exception android.database.sqlite.SQLiteConstraintException:NOT NULL约束失败:HISTORY.altitude(代码1299)”。

所有字段在表中声明为NOT NULL,特别是HISTORY.altitude字段声明为“REAL NOT NULL”

我无法理解它是如何可能为NULL,因为我在insert命令上填充了几行。

任何人都知道如何做到这一点?

synchronized public void addSample( long time, int method, float altitude, int accuracy )
{
    ...
        ContentValues cv = new ContentValues();

        long day = new TimeUtils.Day(time).getMillis();
        cv.put(MetaData.COLUMN_DAY, day);
        cv.put(MetaData.COLUMN_TIME, time);
        cv.put(MetaData.COLUMN_ALTITUDE, altitude);
        cv.put(MetaData.COLUMN_METHOD, method);
        cv.put(MetaData.COLUMN_ACCURACY, accuracy);

        long id = latestSample.mId;
        id = mDb.insertOrThrow(MetaData.TABLE_NAME, null, cv);  // <--- Here crash
    ...
}

这里是用于定义表的代码:

/* Inner class that defines the table contents */
public static abstract class MetaData implements BaseColumns
{
    public static final String TABLE_NAME = "HISTORY";
    public static final String COLUMN_DAY = "day";
    public static final String COLUMN_TIME = "time";
    public static final String COLUMN_ALTITUDE = "altitude";
    public static final String COLUMN_METHOD = "method";
    public static final String COLUMN_ACCURACY = "accuracy";
}

private static final String SQL_CREATE_HISTORY_TABLE =
        "CREATE TABLE " + MetaData.TABLE_NAME + " ( " +
                MetaData._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                MetaData.COLUMN_DAY + " INTEGER NOT NULL, " +
                MetaData.COLUMN_TIME + " INTEGER NOT NULL, " +
                MetaData.COLUMN_ALTITUDE + " REAL NOT NULL, " +
                MetaData.COLUMN_METHOD + " INTEGER NOT NULL, " +
                MetaData.COLUMN_ACCURACY + " INTEGER NOT NULL " +
                " );\n" +
        "CREATE INDEX " + MetaData.COLUMN_METHOD + " ON " + MetaData.TABLE_NAME + " (" + MetaData.COLUMN_METHOD + ");\n" +
        "CREATE INDEX " + MetaData.COLUMN_DAY + " ON " + MetaData.TABLE_NAME + " (" + MetaData.COLUMN_DAY + ");\n" +
        "CREATE INDEX " + MetaData.COLUMN_TIME + " ON " + MetaData.TABLE_NAME + " (" + MetaData.COLUMN_TIME + ");";

1 个答案:

答案 0 :(得分:3)

SQLite不允许使用NaN的浮点值,并将它们视为NULL。 因此,如果您的float值以某种方式结束为NaN,则会违反NOT NULL约束。