I am really stuck here as I have no explanation on why my insertions don't work in my Android project, using Android studio. I have been looking through the SQLiteDatabase documentation on Android Developers and the questions that are related to my problem here on SO, but nothing helped.
The problem I have is I have set an integer ID as primary key which autoincrements itsels. But after inserting my first values, it seems the ID won't increment and it's still at 0 for the second set of values!
About how my app works :
- I've got a fragment with a list on which I can decide to add an item (or update/delete it)
- Adding/updating an item opens a new fragment on which I can enter/edit values
- Adding the first set of values, or editing them works fine
- When I want to add a second set of values, the database error appears in the logcat of Android Studio, I get back from the fragment on which I can insert values to the list, but there is obviously still only one item since the second one couldn't be added
I really can't find what's wrong here, as I think I've covered up the incrementation of my primary key!
Here are my columns declaration from my SQL Helper:
public static class ShipContract {
public ShipContract(){}
public abstract static class ShipEntry implements BaseColumns {
public static final String TABLE_NAME = "ship_configuration";
public static final String COLUMN_NAME_ID = "id";
public static final String COLUMN_NAME_SHIP_NAME = "ship_name";
public static final String COLUMN_NAME_FSD = "fsd_range";
public static final String COLUMN_NAME_CARGO = "cargo_capacity";
public static final String COLUMN_NAME_CREDITS = "credits_available";
public static final String COLUMN_NAME_LAST_SYSTEM = "last_system";
}
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE "+ShipEntry.TABLE_NAME+" ("+
ShipEntry.COLUMN_NAME_ID+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "+
ShipEntry.COLUMN_NAME_SHIP_NAME+" TEXT, "+
ShipEntry.COLUMN_NAME_FSD+" INTEGER, "+
ShipEntry.COLUMN_NAME_CARGO+" INTEGER, "+
ShipEntry.COLUMN_NAME_CREDITS+" INTEGER, "+
ShipEntry.COLUMN_NAME_LAST_SYSTEM+" TEXT"+
")";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "+ShipEntry.TABLE_NAME;
}
That's how I insert a set of values in my SQL Helper :
public void insertShip(ContentValues cv){
getWritableDatabase().insert(ShipContract.ShipEntry.TABLE_NAME, "null", cv);
}
And that's how I prepare my data to be inserted when I click my "save" button from the fragment that allows me to insert values in it:
save.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (id == -1){
db.insertShip(new ShipConfiguration(
shipName.getText().toString().equals("") ? "UNTITLED" : shipName.getText().toString(),
Integer.parseInt(fsdRange.getText().toString()),
Integer.parseInt(cargoCapacity.getText().toString()),
Integer.parseInt(creditsAvailable.getText().toString()),
lastSystem.getText().toString()).getDatabaseInsert());
} else {
sc.setShipName(shipName.getText().toString().equals("") ? "UNTITLED" : shipName.getText().toString());
sc.setFsdRange(Integer.parseInt(fsdRange.getText().toString()));
sc.setCargoCapacity(Integer.parseInt(cargoCapacity.getText().toString()));
sc.setCreditsAvailable(Integer.parseInt(creditsAvailable.getText().toString()));
sc.setLastSystem(lastSystem.getText().toString());
db.updateShip(sc.getDatabaseInsert());
}
getActivity().getSupportFragmentManager().popBackStack();
}
});
Here's the error from the log cat:
06-17 23:44:23.043 13314-13314/ E/SQLiteLog: (1555) abort at 14 in [INSERT INTO ship_configuration(fsd_range,last_system,credits_available,ship_name,_id,cargo_capacity) VALUES (?,?,?,?,?,?)]: UNIQUE constraint failed: ship_configuration._id
06-17 23:44:23.043 13314-13314/ E/SQLiteDatabase: Error inserting fsd_range=666 last_system=Gfd credits_available=699 ship_name=Yogabagaba _id=0 cargo_capacity=666
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: ship_configuration._id (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:926)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1581)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1451)
at space.muscorp.elitedangerousmate.db.SQLHelper.insertShip(SQLHelper.java:233)
at space.muscorp.elitedangerousmate.views.ShipUpdaterFragment$1.onClick(ShipUpdaterFragment.java:65)
at android.view.View.performClick(View.java:4788)
at android.view.View$PerformClick.run(View.java:19965)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5431)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Aucun commentaire:
Enregistrer un commentaire