SQLite AUTOINCREMENT

时间:2015-03-15 23:00:25

标签: java android sqlite

我的表格是这样创建的:

    db.execSQL("CREATE TABLE reminder (" +
            "id INTEGER PRIMARY_KEY AUTO_INCREMENT," +
            "title VARCHAR(100)," +
            "content VARCHAR(500)," +
            "due LONG," +
            "alarm LONG," +
            "priority INT" +
            ");");

和插入的值:

    ContentValues cv=new ContentValues();
    cv.put("title",t.title);
    cv.put("content",t.content);
    cv.put("due",t.date.getTimeInMillis());
    cv.put("alarm",(t.alarm==null?0L:t.alarm.getTimeInMillis()));
    cv.put("priority",t.severity);
    SQLiteDatabase d=getWritableDatabase();
    d.insert("reminder",null, cv);
    d.close();

但是当我试图通过以下方式获得我的价值观时:

        Task task=new Task();
        task.id=c.getInt(0);
        task.title=c.getString(1);
        task.content=c.getString(2);
        Calendar due=new GregorianCalendar();
        due.setTimeInMillis(c.getLong(3));
        task.date=due;
        if(c.getLong(4)!=0){
            Calendar alert=new GregorianCalendar();
            alert.setTimeInMillis(c.getLong(4));
            task.alarm=alert;
        }
        task.severity=c.getInt(5);
        return task;

我的id总是等于零。知道怎么解决吗?

2 个答案:

答案 0 :(得分:1)

下划线有点不合适。试试id INTEGER PRIMARY KEY AUTOINCREMENT

SQL As Understood By SQLite - CREATE TABLE

答案 1 :(得分:-1)

请尝试此解决方案:

db.execSQL("CREATE TABLE reminder (" +
            "'_id' INTEGER PRIMARY_KEY AUTO_INCREMENT," +
            "title VARCHAR(100)," +
            "content VARCHAR(500)," +
            "due LONG," +
            "alarm LONG," +
            "priority INT" +
            ");");

更改了_id的ID。