1

I'm trying to update multiple rows using SQLiteDatabase.update on certain table.

Code to execute query:

public void onJobStart(int fileType) {

    ContentValues cv = new ContentValues();
    cv.put(Contract.Entry.UPLOAD_STATUS, STATUS_READY);

    String whereClause = String.format(
            Locale.US,
            "%s = %d AND %s = %d",
            Contract.Entry.FILE_TYPE,
            fileType,
            Contract.Entry.UPLOAD_STATUS,
            STATUS_CREATED
    );

    int success = mDatabase.update(
            Contract.Entry.TABLE_NAME,
            cv,
            whereClause,
            null
    );

    Log.e(TAG, "success=" + success);

}

Creation of table and it's columns:

public class DbHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "cache.db";


    private DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String table = "CREATE TABLE " + Contract.Entry.TABLE_NAME + " ( " +
            Contract.Entry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            Contract.Entry.UPLOAD_STATUS + " INTEGER, " +
            Contract.Entry.FILE_TYPE + " INTEGER " + ");"

        db.execSQL(table);
    }

    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Contract.Entry.TABLE_NAME);
        onCreate(db);
    }
}

public final class Contract {

    public Contract() {}

    public static abstract class Entry implements BaseColumns {
    public static final String TABLE_NAME = "files";
    public static final String UPLOAD_STATUS = "uploadStatus";
    public static final String FILE_TYPE = "fileType";
    }
}

This code doesn't work and I'm keep gettingsuccess=0 after executing the query. It's kind of weird because till yesterday the where clause was with only one argument and it worked i.e:

String whereClause = String.format(
            Locale.US,
            "%s = %d",
            Contract.Entry.UPLOAD_STATUS,
            STATUS_CREATED
    );

Edit: Also tried this:

String whereClause = Contract.Entry.FILE_TYPE + " = ? AND " + Contract.Entry.UPLOAD_STATUS + " = ?";
    String[] whereArgs = new String[] {String.valueOf(fileType), String.valueOf(STATUS_CREATED)};
    int success = mDatabase.update(
            Contract.Entry.TABLE_NAME,
            cv,
            whereClause,
            whereArgs
    );

Basically I would like to update all rows that their status is created. For couple of hour didn't find a solution here in SO. and google.

Any ideas? Thanks

sharonooo
  • 684
  • 3
  • 8
  • 25

1 Answers1

-1

I recon it is in this part:

"%s = %d AND %s = %d"

try this: (they all need to be String to work tho)

"%1$s = %2$s AND %3$s = %4$s"