简体   繁体   English

Android,SQLite…主键插入为null

[英]Android , SQLite… the primary key is inserted as null

I'm a beginner with android, and trying to create a database. 我是android的初学者,正在尝试创建数据库。 with one table and three functions add , delete, update and retrieve. 具有一个表和三个功能的添加,删除,更新和检索。

When I insert an item to the data base the primary key will be given a number that is incremented each time .. but it is inserted to the database as null ! 当我将一个项目插入数据库时​​,主键将被赋予一个每次递增的数字..但它会作为null插入数据库中!

Therefore I couldn't update and delete records because I need the id. 因此,由于需要ID,因此无法更新和删除记录。 and for the retrieve the output works fine except for the id it is null. 对于检索,输出工作正常,但id为null。

Here is the schema 这是架构

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME= "alarmapp"; 
private static final String CREATE_alarm= "CREATE TABLE alarm" +
        " (Aid integer  PRIMARY KEY NOT NULL AUTOINCREMENT," +
        "message varchar(100)," +
        "date varchar(100));";

and here is the insert code: 这是插入代码:

// columns 
private static final String AI = "Aid";
private static final String Message= "message";
private static final String Date = "date";

// tables
private static final String TABLE_NAME= "alarm";

private Context context;
private DatabaseHelper dbhelper;
private SQLiteDatabase database;

public long add( String message, String date) {
    ContentValues values= new ContentValues();

    values.put(Message,message);
    values.put(Date, date);

    return database.insert(TABLE_NAME, null,values);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM