0

Following is my code for update record done.

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());
            }

Code for Display:

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());

        }

I am getting all variable value i.e global variables and all but update does not reflects on table. What wrong i am done?

Android
  • 8,995
  • 9
  • 67
  • 108

3 Answers3

1

whenever we try to update our database then just clean and uninstall your app then again install may be some changes not take place when we don't uninstall it if u find correct then tell other wise we will see the next

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

Should int notecol = note_cursor.getColumnIndex("name"); instead be int notecol = note_cursor.getColumnIndex("note");

since in the first block of code you are updating note..

Kevin Joymungol
  • 1,764
  • 4
  • 20
  • 30
  • no that my mistake.. i am displaying same... note.. bt not reflacting... :( – Android Feb 14 '14 at 07:03
  • May be you need to check whether there are any trailing spaces in your sqlite name field in the db. If so you need to trim the sqlite name field in the rawQuery. – Kevin Joymungol Feb 14 '14 at 07:11
0

The problem is that you try to update your DB using a rawQuery method instead of a execSql. RawQuery is intended to retrieve data from your database, while execSql lets you

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

You can also consider to use public int update (String table, ContentValues values, String whereClause, String[] whereArgs) method.

user2340612
  • 10,053
  • 4
  • 41
  • 66