Android:不更新sqlite数据库

时间:2013-11-05 05:51:51

标签: java android sqlite

在我的Android应用程序中,有一个预定义的数据库,位于assets文件夹中。它有一个表名WMExecutive Activity。当用户输入数据并单击一个按钮时,我需要更新此表。这是我的活动。

case R.id.btnAdd:
        if (description.getText().toString().trim().equals("")) {
            Toast.makeText(getActivity(), "Description cannot be blank",
                    Toast.LENGTH_SHORT).show();
        } else if (selectedComplain.equals("--Select--")) {
            Toast.makeText(getActivity(), "Select a complain type to proceed",
                    Toast.LENGTH_SHORT).show();
        } else {
            SelectDBAdapter selectDBAdapter = SelectDBAdapter
                    .getDBAdapterInstance(mActivity);
            try {
                selectDBAdapter.openDataBase();
                lstActivity = selectDBAdapter.getActivityDetails();
                System.out.println("lstActivity.size >>>> "+lstActivity.size());

                //Update WMExecutiveActivity table
                //for(int i = 0; i < lstActivity.size(); i++){
                nextActivityNo = lstActivity.get(0).getNextActivityNo();
                complainActivityCatgry = lstActivity.get(0).getActivityCategory().toString();
                //}

                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //updatedOn = sdf.format(sys_date);
                createdOn = sdf.format(sys_date);

                String[] fieldLst = {"BusinessUnit", "ExecutiveCode", "ActivityNo", "ActivityCode", "ActivityType", "RetailerCode", "ProductCode", "ActivityDate", "FollowUpRequiredBefore",
                        "ActivityText", "VisitNumber", "VisitSequence","UpdatedBy", "CreatedOn", "CreatedBy", "UploadFlag", "Status", "ActivityStatus", "RecID"," ComplaintActivityCategory",
                        "ContactInfo", "TerritoryCode", "ComplaintDetail1"};

                String[] valuesLst = {businessUnit, executiveCode, Integer.toString(nextActivityNo), activityCode, activityType, selectedRetCode, prodCode, strDate, date.getText().toString(),
                        selectedComplain, visitNo, visitSequence, executiveCode, createdOn, executiveCode, "1", "1", "0", "0", complainActivityCatgry, 
                        contInfo.getText().toString(), territoryCode, description.getText().toString()};



                DBAdapter dbAdapter = DBAdapter
                        .getDBAdapterInstance(mActivity);
                dbAdapter.openDataBase();

                boolean status = dbAdapter.updateExecActivity(fieldLst, valuesLst, "WMExecutiveActivity");
                dbAdapter.close();
                if (!status) {
                    Toast.makeText(getActivity(),
                            "Inserting data has been unsuccessfully!!! Try Later!!",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(),
                            "Inserting data has been successfully!!!",
                            Toast.LENGTH_SHORT).show();
                }



            } catch (Exception e) {
                selectDBAdapter.close();
                e.printStackTrace();
            } finally {
                selectDBAdapter.close();
            }

这是我的DBAdapter类段。

public boolean updateExecActivity(String[] fieldLst, String[] valuesLst, String tblname) {
    boolean status = true;
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    String updatedOn = sdf.format(date);

    try {
        myDataBase.beginTransaction();
        ContentValues values = new ContentValues();
        for (int i = 0; i < fieldLst.length; i ++){
            values.put(fieldLst[i], valuesLst[i]);
        }
        values.put("UpdatedOn", updatedOn);
        System.out.println("values>>>>"+values.size());

        long exeActivity = myDataBase.update(tblname, values, null, null);
        System.out.println("exeActivity>>>"+exeActivity);
        if(exeActivity == -1){
            System.out.println("status>>>"+status);
            status = false;
            throw new Exception(
                    "There are some error in inserting data to  WMExecutiveActivity");
        }
        myDataBase.setTransactionSuccessful();
    } catch (Exception e) {
        status = false;
        e.printStackTrace();
        Log.i("==DBAdapter==updateExecActivity>>>>"
                + DBAdapter.class.toString(), e.getMessage());
    } finally {
        myDataBase.endTransaction();
    }
    return status;
}

但问题是它没有更新表格。 Update方法返回零。它显示了一个toast 插入数据已成功!!!

任何人都可以解释这有什么问题。

提前致谢

0 个答案:

没有答案