I'm creating an android app that requires database connectivity, for which I'm using the inbuilt SQLiteDatabase class. My database has been created using the following statement:-
SQLiteDatabase db;
db = openOrCreateDatabase("ContactsMain",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("Create Table Contacts (name Text, phno Text PRIMARY KEY, important Integer)");
'important' has values of either 0 or 1, based on whether the contact is important. To update this value, I'm using the following code:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
c.moveToPosition(position);
String ph = c.getString(c.getColumnIndex("phno"));
ContentValues val = new ContentValues();
val.put("important",1);
String query = "update Contacts set important = 1 where phno=" + ph + ";";
db.update("Contacts", val, query, null);
c.close();
db.close();
}
db and c have been initialized as follows:
SQLiteDatabase db = openOrCreateDatabase("ContactsMain",SQLiteDatabase.CREATE_IF_NECESSARY,null);
Cursor c = db.rawQuery("SELECT * FROM Contacts",null);
Problem is, when I click on an item in the list, the value of 'important' is set to 1 for all contacts, not just the one selected. Could someone help me out here?