mardi 22 mars 2016

SQLite not recording values after successful transaction

I'm having this weird situation about inserting a record in a table with autoincrement.

Here is the table:

String CREATE_TABLE_ROUTES = "CREATE TABLE " + TABLE_ROUTES +
                "(" +
                PK_ROUTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                FK_ROUTE_TRACK_ID + " INTEGER," +
                COL_ROUTE_ORDER + " INTEGER(11)," +
                COL_ROUTE_LAT + " FLOAT," +
                COL_ROUTE_LONG + " FLOAT," +
                COL_ROUTE_TIME + " FLOAT," +
                COL_ROUTE_SPEED + " FLOAT," +
                COL_ROUTE_TIME_MS + " INTEGER(3)" +
                ")";

This is my insert method that is not working:

public void updateRoute(LatLongTime latLongTime, long track_id) {

        long route_id = -2;
        SQLiteDatabase db = getWritableDatabase();

        db.beginTransaction();

        try {

            ContentValues values = new ContentValues();
            values.put(FK_ROUTE_TRACK_ID, track_id);
            values.put(COL_ROUTE_ORDER, getLastOrderForTrack(track_id) + 1); 
            values.put(COL_ROUTE_LAT, latLongTime.getLatitude());
            values.put(COL_ROUTE_LONG, latLongTime.getLongitude());
            values.put(COL_ROUTE_SPEED, latLongTime.getSpeed());
            values.put(COL_ROUTE_TIME, latLongTime.getInstante());

            route_id = db.insert(TABLE_ROUTES, null, values);

            db.setTransactionSuccessful();

        } catch (Exception e) {
            Log.e(TAG, "ERROR Recording: " + e.getMessage());
        } finally {
            Log.d(TAG, "Route inserted: " + route_id + " track id: " + track_id);
            db.endTransaction();
            db.close();
        }
    }

Debbuging it I got a successful transaction, but after this method is called again, the same route_id is generated by the insert method. Nothing is recorded after the code runs (with no exception).

This is what I get when I call this function several time at the debug:

D/Database: Route inserted: 9362 track: 103
D/Database: Route inserted: 9362 track: 103
D/Database: Route inserted: 9362 track: 103

What I find most weird is that if I use ctrl+U (Android Studio) and debbug it in a separated thread, this functions start to work correctly and increases the route_id as expected. But only while debugging and repeating this instructions only:

db.beginTransaction();

    try {

        ContentValues values = new ContentValues();
        values.put(FK_ROUTE_TRACK_ID, track_id);
        values.put(COL_ROUTE_ORDER, getLastOrderForTrack(track_id) + 1); 
        values.put(COL_ROUTE_LAT, latLongTime.getLatitude());
        values.put(COL_ROUTE_LONG, latLongTime.getLongitude());
        values.put(COL_ROUTE_SPEED, latLongTime.getSpeed());
        values.put(COL_ROUTE_TIME, latLongTime.getInstante());

        route_id = db.insert(TABLE_ROUTES, null, values);

Aucun commentaire:

Enregistrer un commentaire