如何更新android数据库中表的值?在下面的代码中,它被插入但未更新,如果更新,则所有值都显示为0.此代码有什么问题
private void InsertINcurrentinfo(int iNCallsToLN, int iNLocalCallsM,
int iNLocalCallsL, int iNSTDCallsHM, int iNSTDCallsM,
int iNSTDCallsL, int iNgp1, int iNgp2, int iNgp3, int iNSplCalls) {
// TODO Auto-generated method stub
try {
myDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = myDB.query(TABLE_CURRENTINFO, null, null, null,null, null, null);
int cnt=cursor.getCount();
cursor.close();
ContentValues initialValues = new ContentValues();
initialValues.put("value1", String.valueOf(iNCallsToLN));
initialValues.put("value2", String.valueOf(iNLocalCallsM));
initialValues.put("value3", String.valueOf(iNLocalCallsL));
initialValues.put("value4", String.valueOf(iNSTDCallsHM));
initialValues.put("value5", String.valueOf(iNSTDCallsM));
initialValues.put("value6", String.valueOf(iNSTDCallsL));
initialValues.put("value7", String.valueOf(iNgp1));
if(cnt==0){
myDB.insert(TABLE_CURRENTINFO, null, initialValues);
}else{
myDB.update(TABLE_CURRENTINFO, initialValues, null, null);
}
if (myDB != null) {
myDB.close();
}
}catch(Exception ex)
{
if (myDB != null) {
myDB.close();
}
Toast.makeText(getBaseContext(), "Error occured while updating Current CallInfo"+ex.toString(),
Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
尝试在startManagingCursor(cursor);
int cnt=cursor.getCount();