如何使用android更新sqlite表中的行项?

时间:2017-08-24 10:17:35

标签: android sqlite

我正在开发一个应用程序,我希望使用旧值更新记录,但是当我尝试更新时,它还会再次显示旧值。我在行项目中有字符串数组,我想在我的应用程序中使用新值更新此字符串数组,即我必须在应用程序中使用新值显示旧值。但在我的代码中我显示更新,但它重复旧的值?我该如何解决这个问题?我已经在执行此操作的活动中导航。

示例:我有像

这样的字符串数组
str1[] str1= {"1","2","3","4"};
str2[] str2= {"a", "b","c","d"};

现在我必须使用像

这样的新值来更新它
str1[] str1= {"1","2","3","4","6", "8"};
str2[] str2= {"a", "b","c","d", "j", "l"};

我的输出显示在当前代码中

str1[] str1= {"6", "8","1","2","3","4","1","2","3","4"};
str2[] str2= {"j", "l","a", "b","c","d", "a", "b","c","d"};

// list values

    private void checkButtonClick() {

        ArrayList<String> list1= new ArrayList<>();
        ArrayList<String> list2= new ArrayList<>();

        Poses country = null;
        ArrayList<Poses> countryList = dataAdapter.countryList;
        for (int i = 0; i < countryList.size(); i++) {
            country = countryList.get(i);
            if (country.isSelected()) {
                // responseText.append("\n" + country.getName());
                list1.add(country.getName());
                list1.add(convertArrayToString(subject));
                list2.add(country.getCode());
                list2.add(convertArrayToString(spinner));
            }
        }
        final String [] names = list1.toArray(new String[list1.size()]);
        final String [] spinner = list2.toArray(new String[list2.size()]);

        dbManager.update(id1, names, spinner);
    }

//db values query

 public int update(long _id, String [] selected_pgm, String [] spinner_value) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.SELECTED, convertArrayToString(selected_pgm));
        contentValues.put(DatabaseHelper.SPINNER, convertArrayToString(spinner_value));
        int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
        return i;
    }

1 个答案:

答案 0 :(得分:-1)

您可以直接使用rawQuery方法进行更新。 参考:

https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

例如 -

String updateQuery ="UPDATE myTable SET xyz = 1 WHERE id_abc = " + jkl+"";
Cursor c= dbManager.RawQuery(updateQuery, null);
c.moveToFirst();
c.close();