lundi 23 février 2015

SQLitedatabase not inserting id for lollipop

I have the following code to insert contentValues in Database. I use the SQLitedatabase method insert() to insert values in database as follows:



public static final String ID = "id";
public boolean replaceOrUpdate(DBListener dbListener,final String sTable, ContentValues[] contentValues,String tag) {
this.mDbListener=dbListener;

if(mDatabase == null) {
openDB();
}
mDatabase.beginTransaction();
try {
int count = contentValues.length;
for (int i=0; i<count;i++) {
ContentValues value = contentValues[i];
long id = mDatabase.replaceOrThrow(sTable,null,value);
}
mDatabase.setTransactionSuccessful();
mDbListener.onCompleteInsertion(tag);
}catch (Exception e) {
Log.v("Exception = " , " " + e.toString());
e.printStackTrace();
}finally {
mDatabase.endTransaction();
}
return true;
}


public ArrayList<String> getAllPlacesIDs()
{
ArrayList<String> ids = new ArrayList<String>();
try
{
if(mDatabase==null || !mDatabase.isOpen())
{
openDB();
}
String query="SELECT "+DBUtil.ID+" FROM "+TABLE_MERCHANT_STORES;
Cursor cursor = mDatabase.rawQuery(query,null);
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
{
ids.add(cursor.getString(cursor.getColumnIndex(ID)));
}
cursor.close();
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{

}
return ids;

}


The table is created as follows:



db.execSQL("CREATE TABLE IF NOT EXISTS "+
TABLE_MERCHANT_STORES +" ("+
AUTO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
ID +" INTEGER NOT NULL UNIQUE, "+...)


This code works fine on pre-lollipop devices, but the insertion does not work as expected on lollipop. It seems to get stuck at certain values and continues inserting the missing values in the table. After insertion when I print all the values they appear in ascending order with a few intermediate values missing. The id values stored in the table are


1 2 3 4 5 7 11 13 14 15 16 17 19


whereas the values that gets inserted (expected) on pre-lollipop are:


514 533 531 312 434 162 253 252 151 344 153 160 658


The error pattern seen on lollipop devices is that when an id is inserted in the table, it seems that the query continues inserting in the gaps between last greatest ID and current greatest id as long as the next id it receives is not greater than the current id inserted. Is this a bug in lollipop sqlite? Or is it something specific to my device? There is no such logic implemented in the app. How do I correct this? Is it because the string being used is id? It works fine on pre-lollipop devices, but works wrongly on lollipop.


Aucun commentaire:

Enregistrer un commentaire