View Issue Details

IDProjectCategoryView StatusLast Update
0003975SymmetricDSBugpublic2025-06-04 17:25
Reporterelong Assigned To 
Prioritynormal 
Status acknowledgedResolutionopen 
Product Version3.8.0 
Summary0003975: Android SQLITE_CONSTRAINT_PRIMARYKEY
DescriptionUser reported that an update will sometimes get 0 rows affected, which causes a fallback to an insert, which then returns SQLITE_CONSTRAINT_PRIMARYKEY because the row already exists. The call to select changes() to return the affected rows is returning 0 in some cases. Code re-implementation for AndrdoidSqlTemplate.update(SQLiteDatabase database, String sql, Object[] values, int[] types) is provided here:

https://sourceforge.net/p/symmetricds/discussion/1354726/thread/664087b442/
Tagsdialect: android

Activities

pmarzullo

2025-06-04 17:25

developer   ~0003008

if (values != null) {

            // no longer using execSQL as not able to reliably get number of rows updated using select changes() afterward
            //database.execSQL(sql, toStringArray(values));

            android.database.sqlite.SQLiteStatement statement = database.compileStatement(sql);

            //statement.bindAllArgsAsStrings(toStringArray(values)); <-- cannot bind null values
            for (int i = 0; i < values.length; i++) {
                String value = toStringArray(values)[i];
                if (value != null) {
                    statement.bindString(i+1, value);
                }
                else {
                    statement.bindNull(i+1);
                }
            }

            numberRowsUpdated = statement.executeUpdateDelete();

        } else {
            // no longer using execSQL as not able to reliably get number of rows updated using select changes() afterward
            //database.execSQL(sql);

            android.database.sqlite.SQLiteStatement statement = database.compileStatement(sql);
            numberRowsUpdated = statement.executeUpdateDelete();

        }

        // For some reason neither query below is always returning the number of rows updated
        //int numberRowsUpdated = queryForObject(database, "select changes()", Integer.class);
        //long numberRowsUpdated = android.database.DatabaseUtils.longForQuery(database, "SELECT changes()", null);

        Log.i(TAG, "update() - number rows updated =<" + numberRowsUpdated + "> when executing SQL =<" + sql + ">");
        return numberRowsUpdated;

Issue History

Date Modified Username Field Change
2019-05-22 16:47 elong New Issue
2019-05-22 16:47 elong Tag Attached: dialect: android
2025-06-04 17:25 pmarzullo Note Added: 0003008
2025-06-04 17:25 pmarzullo Status new => acknowledged