插入数据库时​​出现 SQLiteConstraintException

SQLiteConstraintException when inseting to database

我试图将数据插入 android 中的数据库。

这个有效:

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table study " +
                    "(percentage integer primary key,videoDuration text,totalTime text,date text)"
    );
}


public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("percentage", percentage);
    contentValues.put("videoDuration", videoDuration);
    contentValues.put("totalTime", totalTime);
    contentValues.put("date", date);
    db.insert("study", null, contentValues);
    return true;
}

但是当我尝试为序列号添加一列时(因为我没有任何主键列),我在插入时出错。

这是新代码(无效):

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table study " +
                    "(serial integer primary key,percentage text,videoDuration text,totalTime text,date text)"
    );
}


public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
    serialNumber++;
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("serial", serialNumber);
    contentValues.put("percentage", percentage);
    contentValues.put("videoDuration", videoDuration);
    contentValues.put("totalTime", totalTime);
    contentValues.put("date", date);
    db.insert("study", null, contentValues);
    return true;

}

我将 serialNumber 用作计数器,这样它可以连续插入值并充当主键。我收到此代码的错误:

Error inserting date=27/08/2016 percentage=2.6 serial=1 totalTime=0:0:1 videoDuration=22:96
android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

我不明白为什么第一个有效但第二个无效。我想了解为什么第二个代码不起作用。

我的应用程序可以使用第一个代码,但它可能会在未来导致错误,所以我想使用第二个代码。

序列栏只接受唯一值。

刚更新

db.execSQL("create table study (serial integer,percentage text,videoDuration text,totalTime text,date text)");

百分比不能作为主键,如错误提示它应该是唯一的,推荐的做法是添加id字段。

db.execSQL(
"create table study " +
"(id integer primary key, percentage,videoDuration text,totalTime text,date text)"
);

尝试一下,添加自动增量并手动删除 id 的设置:

db.execSQL(
        "create table study " +
                "(serial integer primary key AUTOINCREMENT ,percentage text,videoDuration text,totalTime text,date text)"
);

public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("percentage", percentage);
    contentValues.put("videoDuration", videoDuration);
    contentValues.put("totalTime", totalTime);
    contentValues.put("date", date);
    db.insert("study", null, contentValues);
    return true;
}

public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("percentage", percentage);
    contentValues.put("videoDuration", videoDuration);
    contentValues.put("totalTime", totalTime);
    contentValues.put("date", date);
    db.insert("study", null, contentValues);
    return true;
}

在创建 table 时获取 id 并将其作为主键自动递增:

   db.execSQL("create table study (id integer primary key autoincrement, serial integer,percentage text,videoDuration text,totalTime text,date text)"
);