I\'m trying to create the following database:
public static final String KEY_NAME = \"nombre\";
public static final String KEY_ROWID = \"_id\";
public static fin
I haven't worked with Android, so this could be off, but it appears you're sending the ID with the insert into CAPAS table, but it's unnececessary because you have it defined as AUTOINCREMENT.
You have:
public long createCapa(String id, String nombre) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ID, id);
initialValues.put(KEY_NAME, nombre);
return mDb.insert(DATABASE_TABLE_CAPAS, null, initialValues);
}
What happens if you do this?
public long createCapa( String nombre) {
ContentValues initialValues = new ContentValues();
//initialValues.put(KEY_ID, id);
initialValues.put(KEY_NAME, nombre);
return mDb.insert(DATABASE_TABLE_CAPAS, null, initialValues);
}
and this:
createCapa("Escuelas");
Is it really common practice to define both _id and id in Android? When you define the pk as integer primary key in SQLite it becomes an alias for the rowid automatically. Here's how I would do it normally:
create table capas (id integer primary key autoincrement, nombre text not null);
YOU can try this it worked for me when i was getting same error for no reason
return mDb.insertWithOnConflict(DATABASE_TABLE_CAPAS, null,
initialValues,SQLiteDatabase.CONFLICT_REPLACE);
Hm...try this idea:
Have you tried to allow writing to the database before you do the writing? SQLiteDatabase.OPEN_READWRITE
?
Like
mDb=SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READWRITE);
?
The first thing I think of with "constraint failed" is that something is either being inserted a NULL or you're missing a column. Nothing is being inserted as NULL in that example, and the only missing column is an autoincrement primary key.
However, unless you indicate it in some way, the database and tables are not recreated on each test. Could you be testing on a device that has an older version of those tables in it, one where the "_id" column was not autoincrement? If so, it's expecting ContentValues to also contain a value for that column.
I tried all your suggestions during these 2 days and I appreciate them. But you know what? When I started Eclipse today, I run the app and I just worked out. I have no idea how, because yesterday when I finished trying it didn't work. But surprisingly it's fixed.
I'm quite happy about that, but also concerned, because that makes me feel the app is not reliable. Does these things happen usually in Android or in SQLite? Has it happened to you before? If you have some advice, please share it, because maybe it will stop working suddenly some other day, who knows?