Android SQLite。插入。 android.database.sqlite.SQLiteConstraintException

时间:2011-07-25 02:29:30

标签: android sqlite

创建数据库代码

    StringBuilder my_db = new StringBuilder();
    my_db.append("create table " + "my_db" + " (");
    my_db.append("id integer unique," +
                                  "tmp_id integer unique," +
                                  "tmp_at datetime ," +
                                  "tmp_count integer," +
                                  "tmp_reason_code integer);");
    db.execSQL(unlocked_achievements.toString());

插入代码

final String sql = "insert into my_db (id, tmp_reason_code, tmp_count, tmp_id) values (?, ?, ?, ?)";
SQLiteDatabase db = LocalDB.getInstance().getWritableDatabase();
db.beginTransaction();
try {
    SQLiteStatement stmt = db.compileStatement(sql);
    stmt.bindLong(1, id);
    stmt.bindLong(2, 0);
    stmt.bindLong(3, 0);
    stmt.bindLong(4, tmp_id);
    stmt.execute();
    db.setTransactionSuccessful();
} catch (SQLException e) {
    e.printStackTrace();
}
db.endTransaction();
db.close();

如果插入代码启动,则生成android.database.sqlite.SQLiteConstraintException。

1 个答案:

答案 0 :(得分:0)

表格定义中包含以下内容:

id     integer unique
tmp_id integer unique

因此,您的表格中已经有idtmp_id,并且尝试再次插入它们会违反这些列的唯一约束。