Android sqlite更新未在表中完成

时间:2014-02-14 06:46:07

标签: android sqlite record updating

以下是我完成更新记录的代码。

try {
                String str_edit = edit_note.getText().toString();
                Toast.makeText(getApplicationContext(), str_edit,
                        Toast.LENGTH_LONG).show();
                                    Toast.LENGTH_LONG).show();
        String      rawQuery="UPDATE " + GlobalVariable.getstr_tbl_name()
                        + " SET note = '" + str_edit + "' where name = '"
                        + str + "' ";
                db.execSQL(rawQuery);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

显示代码:

try {
            Cursor note_cursor = db.query(GlobalVariable.getstr_tbl_name(),
                    new String[] { "note" + " as note" }, "name" + "=?",
                    new String[] { GlobalVariable.getstr_food_name() }, null,
                    null, null);
            if (note_cursor.moveToFirst()) {
                int notecol = note_cursor.getColumnIndex("note");
                do {
                    String str = note_cursor.getString(notecol);
                    Toast.makeText(getApplicationContext(), str,
                            Toast.LENGTH_LONG).show();
                    edit_note.setText(str);
                } while (note_cursor.moveToNext());

            }
        }

        catch (Exception e) {
            System.out.println(e.getMessage());

        }

我得到所有变量值,即全局变量,除了更新之外的所有变量都没有反映在表格上。 我做错了什么?

3 个答案:

答案 0 :(得分:1)

每当我们尝试更新我们的数据库然后只是清理并卸载你的应用程序然后再次安装可能会发生一些变化,如果我们没有卸载它,如果你找到正确的话告诉其他明智的我们会看到下一个

答案 1 :(得分:0)

应该     int notecol = note_cursor.getColumnIndex("name"); 而是     int notecol = note_cursor.getColumnIndex("note");

因为在第一个代码块中你正在更新note ..

答案 2 :(得分:0)

问题是您尝试使用rawQuery方法而不是execSql更新数据库。 RawQuery旨在从您的数据库中检索数据,而execSql则允许您

  

执行一个非SELECT的SQL语句或任何其他返回数据的SQL语句。

您还可以考虑使用public int update (String table, ContentValues values, String whereClause, String[] whereArgs)方法。