0

i am using this code to update the values

this Code is working properly without an error there is no error in query but when i show data after updating its showing the old data??

this is the code i am using for updating the data in sqlite.

  @Override
    public void onClick(View v) {
        if (v == Update) {
            try {
                ContentValues CV = new ContentValues();
                CV.put("cname", EditName.getText().toString());
                CV.put("mobileno", EditMobile.getText().toString());
                CV.put("phone", EditLandline.getText().toString());
                CV.put("address", EditAddress.getText().toString());
                CV.put("email", EditEmail.getText().toString());
                CV.put("picture", Editbytes);
                DB.update("contacts", CV, "cname=" + V,null);

                Intent I = new Intent(ctx, ContactList.class);
                ctx.startActivity(I);


                // DB.execSQL("insert into contacts values('" + Name.getText() + "','" + Mobile.getText() + "','" + Phone.getText() + "','" + Add.getText() + "','" + Email.getText() + "')");
                Toast.makeText(ctx, "Contact Updated...",Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(ctx, "Fail to Update Contact.." + e, Toast.LENGTH_LONG).show();

            }

        } else if (v == Delete) {

        }
    }
jyoti singh
  • 37
  • 1
  • 4

2 Answers2

2

Can you please try this,

DB.update("contacts", CV, "cname=" + V,null);

What is the V, is that particular name , I thingk, you may try to udapte using id, which is more effective.

Change to

SQLiteDatabase db = dbHelper.getWritableDatabase();
db.update("contacts", CV, "cid=" + "=" + id, null);
db.close(); // Closing database connection

However, if you don't update, please clean your project and rebuild, that might work.If this will not help, please post you logcat.

Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
0

Is there any chance WHERE clause is not matching any rows?

You can test your condition on console with:

Log.i(db.execSQL("SELECT * FROM contacts WHERE cname=" + V));

But I think the best way to test this is connect directly to database:

  • Open a terminal on Android Studio (View > Tool Windows > Terminal)
  • Enter adb shell (if command fails, add your adb installation into PATH variable)
  • The database file is usually stored in /data/data/com.your.package/database/
  • Run sqlite3 /data/data/com.your.package/database/YourAPP.db to open a prompt where you can run all SQL commands

Another good answer

efvi
  • 44
  • 5