0

I am using sqlite to maintain database in my android application.Other operations are working fine but the Update Operation is not working.This is the code:

public int updateContact(Contact contact,String Type) 
    {
        int a = 0;
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME, contact.getName());
        values.put(KEY_PH_NO, contact.getPhoneNumber());
        values.put(KEY_TIME, contact.getTime());
        values.put(KEY_ID , contact.getID());

        if(Type == "name")
        {
            a = db.update(TABLE_CONTACTS, values, KEY_NAME + " = ?",
                    new String[] { String.valueOf(contact.getName()) });
        }

        else if(Type == "id")
            // updating row
            a =  db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                    new String[] { String.valueOf(contact.getID()) });

        else if(Type == "number")
            // updating row
            a= db.update(TABLE_CONTACTS, values, KEY_PH_NO + " = ?",
                    new String[] { String.valueOf(contact.getPhoneNumber()) });

        else if(Type == "time")
            a= db.update(TABLE_CONTACTS, values, KEY_TIME + " = ?",
                    new String[] { String.valueOf(contact.getTime())}); 

        return a;
    }

Please help me.

user1726619
  • 941
  • 2
  • 18
  • 39
  • What is the problem? Do you get an exception or is the database not changed in the way you wish? – JesperB Oct 16 '12 at 15:24
  • Actually sir,there is not any exception,the code compiles fine,but i am not getting the desired result i.e the Update Operation is not making any change in the database. – user1726619 Oct 16 '12 at 15:29
  • 2
    Three things: 1. Check that the method is actually being called (Log to logcat). 2. Try logging the parameters passed on to `db.update()` to check if there is anything wrong with these. 3. Check that the table actually contains rows that matches these parameters – JesperB Oct 16 '12 at 15:41

2 Answers2

2

String comparison always should use .equals() instead of ==

if(Type == "name")
......
else if(Type == "number")

should be changed to

    if("name".equals(Type))
  ......
   else if("number".equals(Type))
Lo Juego
  • 1,305
  • 11
  • 12
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Sorry sir, but this is not the problem.Thanks for reply. – user1726619 Oct 16 '12 at 15:27
  • You should, however, always do string comparison this way. Not doing so can lead to subtle bugs that are very hard to discover and reproduce. See why [here](http://stackoverflow.com/a/995933/1319061) – JesperB Oct 16 '12 at 15:29
  • "Update Operation is not making any change in the database", are you sure update is being called? I strongly suspect update is not being called. By the way Sir is somehow banned word in SO. – kosa Oct 16 '12 at 15:30
  • Yes sir,update is being called and it is compiling fine without any errors but not giving the desired result. – user1726619 Oct 16 '12 at 15:37
  • compiling fine has no effect (it compiles fine with ==, but fails runtime). Can you share how you are validating "update is being called"? Can you add either System.out inside if/else blocks and see you are getting those System.out in logcat? – kosa Oct 16 '12 at 15:40
1

Why don't you use this way:

 if(Type == "name")
    {
        a = db.update(TABLE_CONTACTS, values, KEY_NAME + "=" + String.valueOf(contact.getName()),null);
    }

    else if(Type == "id")
        // updating row
        a =  db.update(TABLE_CONTACTS, values, KEY_ID + "=" + String.valueOf(contact.getID()),null);

    else if(Type == "number")
        // updating row
        a= db.update(TABLE_CONTACTS, values, KEY_PH_NO + "=" + String.valueOf(contact.getPhoneNumber()),null);

    else if(Type == "time")
        a= db.update(TABLE_CONTACTS, values, KEY_TIME + "=" + String.valueOf(contact.getTime()),null);
Lo Juego
  • 1,305
  • 11
  • 12
  • Sorry sir,the problem is still same. – user1726619 Oct 16 '12 at 15:34
  • 1
    Have you declared your keys to INTEGER PRIMARY KEY? Also I think there is some issues in SQLite if you don't declare your primary key as: _id – Lo Juego Oct 16 '12 at 15:43
  • @Override public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT," +KEY_TIME+" TEXT" + ")"; db.execSQL(CREATE_CONTACTS_TABLE); } – user1726619 Oct 16 '12 at 15:46