我有以下代码在数据库中插入contentValues。我使用SQLitedatabase方法insert()在数据库中插入值,如下所示:
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(DBUtil.ID)));
}
cursor.close();
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
}
return ids;
}
该表创建如下:
db.execSQL("CREATE TABLE IF NOT EXISTS "+
TABLE_MERCHANT_STORES +" ("+
AUTO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
DBUtil.ID +" INTEGER NOT NULL UNIQUE, "+...)
此代码适用于棒棒糖前设备,但插入在棒棒糖上无法正常工作。它似乎陷入某些值并继续在表中插入缺失值。插入后,当我打印所有值时,它们以升序显示,缺少一些中间值。存储在表中的id值是
1 2 3 4 五 7 11 13 14 15 16 17 19
而在棒棒糖前插入(预期)的值是:
514 533 531 312 434 162 253 252 151 344 153 160 658
在棒棒糖设备上看到的错误模式是,当在表中插入id时,似乎查询继续插入最后一个最大ID和当前最大id之间的间隙,只要它接收的下一个id不大比插入的当前id。这是棒棒糖sqlite中的一个错误吗?或者它是我的设备特有的东西?应用程序中没有实现这样的逻辑。我该如何纠正?是因为正在使用的字符串是id吗?它在棒棒糖前设备上工作正常,但在棒棒糖上工作错误。
编辑:
我添加值的方式如下:
private void insertIntoDB() {
int count = jsonArray.length();
ContentValues[] values = new ContentValues[count];
for(int i=0;i<count;i++){
JSONObject storeObject=jsonArray.getJSONObject(i);
if(storeObject!=null){
ContentValues value=new ContentValues();
value.put(DBUtil.ID, storeObject.getInt("id"));
mArrId.add(storeObject.getInt("id")+"");
value.put("place_parent", storeObject.getInt("place_parent"));
}
}
Cursor c1=new MylocalCursorLoader(mContext, "Delete from " + DBUtil.TABLE_MERCHANT_STORES).loadInBackground();
if(c1!=null){
DBUtil dbUtil = DBUtil.getInstance(mContext.getApplicationContext());
dbUtil.replaceOrUpdate(this, DBUtil.TABLE_MERCHANT_STORES, values,"merchant-stores");
mProgess.setVisibility(View.VISIBLE);
}
}
MyLocalCursorLoader是一个扩展CursorLoader的类。 storeObject.getInt(“id”)的值为
514 533 531 312 434 162 253 252 151 344 153 160 658
答案 0 :(得分:0)
在没有看到您的所有代码的情况下,问题似乎与ContentValues[]
传递给replaceOrUpdate()
方法有关。
您可以在方法中向循环添加日志行,以验证是否是这种情况:
for (int i=0; i<count;i++) {
ContentValues value = contentValues[i];
long id = mDatabase.replaceOrThrow(sTable,null,value);
// Check output between Lollipop and earlier platform versions
Log.v("DBUtil", "replaceOrThrow() returned " + id + "; value=" + value.toString());
}
否则,如果您可以发布填充ContentValues[]
的代码,这可能会帮助您更快地获得更直接的答案。
答案 1 :(得分:0)
我找到了解决方法。如果我在表中创建DBUtil.ID属性时删除UNIQUE,那么它可以正常工作。我仍然不知道确切的原因。如果有人能澄清这种荒谬行为的原因,我将不胜感激。