使用新表值进行数据库更新

时间:2011-11-15 10:59:33

标签: android database

如何更新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();

    }

}

1 个答案:

答案 0 :(得分:0)

尝试在startManagingCursor(cursor);

之前添加int cnt=cursor.getCount();