create db code
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());
insert code
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();
If the insert code starts, android.database.sqlite.S开发者_开发知识库QLiteConstraintException is generated.
You have these in the table definition:
id integer unique
tmp_id integer unique
So you already have id
or tmp_id
in your table and trying to insert them again is violating the unique constraints on those columns.
精彩评论