SQlite主键:数据类型不匹配

时间:2016-07-05 14:39:04

标签: android database sqlite

关于sqlite我有read a bit因为我需要能够在我的表格中使用autoincrement primary key列。

他们提供信息:

  

如果您将表的列声明为INTEGER PRIMARY KEY,那么   无论何时向表的该列插入NULL,NULL都是   自动转换为一个大于的整数   该列中所有其他行的最大值,如果为1,则为1   桌子是空的。

我尝试将NULL插入主键列并遇到以下问题:

[INSERT INTO maschinen(name,id_maschine,description,id_maschine_local_row) VALUES (?,?,?,?)] datatype mismatch
E/SQLiteDatabase: Error inserting name=FLATTENER No. 1 id_maschine=6 description=Flattener the BIG one id_maschine_local_row=NULL

我的创建声明:

CREATE TABLE maschinen(id_maschine_local_row INTEGER PRIMARY KEY AUTOINCREMENT, id_maschine INTEGER, name STRING, description STRING)

我做错了什么?

如果我在没有将一些数据放入插入语句vor id_maschine_local_row的情况下插入一行,则它始终为0.

数据库处理器:

public class DatabaseHandler extends SQLiteOpenHelper {

    // Machines
    private static final String TABLE_MASCHINEN = "maschinen";

    private static final String KEY_MASCHINE_LOCAL_ROW_ID = "id_maschine_local_row";
    private static final String KEY_MASCHINE_ID = "id_maschine";
    private static final String KEY_MASCHINE_NAME = "name";
    private static final String KEY_MASCHINE_DESCRIPTION = "description";

    private String CREATE_MASCHINEN_TABLE = "CREATE TABLE " + TABLE_MASCHINEN + "(" + KEY_MASCHINE_LOCAL_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_MASCHINE_ID + " INTEGER, "+ KEY_MASCHINE_NAME + " STRING, " + KEY_MASCHINE_DESCRIPTION + " STRING)" ;

    (...)
     public long addMaschine(Maschine mMaschine) {
        SQLiteDatabase db = this.getWritableDatabase();

        // machine data
        ContentValues valuesMachine = new ContentValues();
        valuesMachine.put(KEY_MASCHINE_LOCAL_ROW_ID, "NULL"); /* Without that line for each machine the local row id is 0. With that line as suggested in the article I get the issue */
        valuesMachine.put(KEY_MASCHINE_ID, mMaschine.getId());
        valuesMachine.put(KEY_MASCHINE_NAME, mMaschine.getName());
        valuesMachine.put(KEY_MASCHINE_DESCRIPTION, mMaschine.getDescription());

        //characteristics data
        List<Characteristic> characteristicList = new ArrayList<Characteristic>();
        characteristicList.addAll(mMaschine.getCharacteristics());

        for(int i = 0; i < mMaschine.getCharacteristics().size(); i++){
            ContentValues valuesCharacteristics = new ContentValues();

            //valuesCharacteristics.put(KEY_CHARACTERISTIC_LOCAL_ROW_ID, "NULL");
            valuesCharacteristics.put(KEY_CHARACTERISTIC_ID, characteristicList.get(i).getCharacteristic_id());
            valuesCharacteristics.put(KEY_CHARACTERISTIC_ID_MASCHINE, characteristicList.get(i).getId_maschine());
            valuesCharacteristics.put(KEY_CHARACTERISTIC_NAME, characteristicList.get(i).getName());
            valuesCharacteristics.put(KEY_CHARACTERISTIC_DESCRIPTION, characteristicList.get(i).getDescription());
            valuesCharacteristics.put(KEY_CHARACTERISTIC_TYPE, characteristicList.get(i).getType());
            valuesCharacteristics.put(KEY_CHARACTERISTIC_VALUE, characteristicList.get(i).getValue());
            valuesCharacteristics.put(KEY_CHARACTERISTIC_IS_CHANGED, characteristicList.get(i).getIsChanged());

            db.insert(TABLE_CHARACTERISTICS, null, valuesCharacteristics);
        }

        long machine_id = db.insert(TABLE_MASCHINEN, null, valuesMachine);

        db.close();

        return machine_id;
    }
}

编辑: 像这样插入null会产生:

enter image description here

1 个答案:

答案 0 :(得分:2)

AUTOINCREMENT已归档未插入值系统自动输入

INSERT INTO maschinen(name,id_maschine,description,id_maschine_local_row) VALUES (?,?,?,?)

- &GT;     INSERT INTO maschinen(name,id_maschine,description)VALUES(?,?,?)

- &GT; input seq create table filed seq equals

INSERT INTO maschinen(id_maschine,name,description) VALUES (?,?,?)