What does this line mean in my code "id =?",new String[]{id} .I have been trying find out on the internet but I can't seem to find an answer.
public boolean updateData(String id,String name,String quantity,String category,String importance ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, quantity);
contentValues.put(COL_4, category);
contentValues.put(COL_5, importance);
Log.d("data","id="+id);
Log.d("name",name);
Log.d("quantity",quantity);
Log.d("category",category);
Log.d("importance",importance);
db.update(Table_Name,contentValues,"id =?",new String[]{id});
return true;
}
These are in fact two distinct arguments to function SQLiteDatabase.update. The first ("id =?") is an update condition. This condition filters rows to update by a particular criteria, in this case by column id. The question mark is later subsituted by an argument in the next parameter, that defines an array of String objects that should be used for substitution of the placeholders (in this case question marks).
Related
I have a hidden database with 7 columns, one of which is a price and whose values are null. I want to enter some value in the field column for id = 1, then for id = 2 I enter another value etc. and when I fill in the price column I want to update the whole. Is that possible?
My code updates only one value
public boolean updateData(String id,String name, String number_phone, String about, Integer price, String color){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, number_phone);
contentValues.put(COL_4, about);
contentValues.put(COL_5, price);
contentValues.put(COL_6, color);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
I don't quite get your question but from what i understand this could might help you out.
If you want only one field values to be updated then you need to use for loop.
In onCreate method call this with the array of prices that you want to be updated in table's each row.
Array of prices should not be greater than no. of rows exist in table.
for(int i=0;i<(no. of rows in table);i++){
updateData(rowId,priceArray[i]);
}
Now the update method will be like.
public boolean updateData(String id, Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
If you want to update certain rows for example {1,4,5} then you have to update it one by one by calling updateData method with id and prices .
But if you want same value of price field to be updated in certain rows like above {1,4,5} than you can
public boolean updateData(Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{1,4,5});
return true;
}
public long createCountry(String code, String name, String continent,
String region) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PRODUCT_TYPE, code);
initialValues.put(KEY_PRODUCT_NAME, name);
initialValues.put(KEY_PRODUCT_QUANTITY, continent);
initialValues.put(KEY_PRODCUT_COST, region);
return mDb.insertWithOnConflict(SQLITE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);
}
this is for insert or Update function but its taking new Row if name X is alredy present in table row i want it should Update Like suppose we have 3 Entry whose name column value has X,Y,Z now Suppose i want call again this function with name X then rest of value Should update not its should create another Column please help me where am doing wrong .
There is no update-or-insert function.
You have to do this manually:
public void createCountry(String code, String name, String continent, String region) {
ContentValues values = new ContentValues();
values.put(KEY_PRODUCT_TYPE, code);
values.put(KEY_PRODUCT_QUANTITY, continent);
values.put(KEY_PRODCUT_COST, region);
int updated = mDb.update(SQLITE_TABLE, values,
KEY_PRODUCT_NAME + " = ?",
new String[]{ name });
if (updated == 0) {
values.put(KEY_PRODUCT_NAME, name);
mDb.insert(SQLITE_TABLE, null, values);
}
}
public void insertData(String cateogry, String status, String feedback,
String date, Bitmap bitmap) {
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(CATEGORY, cateogry);
cv.put(STATUS, status);
cv.put(FEEDBACK, feedback);
cv.put(IMAGE, Utility.getBytes(bitmap));
cv.put(DATE, date);
db.insert(INFR_TABLE, CATEGORY, cv);
}
public void updateData(String cateogry, String status, String feedback,
String date, Bitmap bitmap)
{
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(CATEGORY, cateogry);
cv.put(STATUS, status);
cv.put(FEEDBACK, feedback);
cv.put(IMAGE, Utility.getBytes(bitmap));
cv.put(DATE, date);
db.update(INFR_TABLE, cv, "cateogry="+cateogry, null);
}
this is update and insert funcation in sqlite i want when row of cateogry is alredy there then update it with other value if not then insert . and i am calling like this way :
for(table)
String data[] = dh.getAllCategory();
if (Arrays.asList(data).contains("Table")) {
dh.updateData("Table", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
else {
dh.insertData("Table", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
for(water) :
String data[] = dh.getAllCategory();
// dh.deleteitem(11);
if (Arrays.asList(data).contains("Table")) {
dh.updateData("Water", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
else
{
dh.insertData("Water", t, j, "25.5.2014", BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
}
but every time its create new row and duplicate please help me where am doing wrong we have 2 class in on is for table and another for water. please tell
public void insertData(String id, String cateogry, String status,
String feedback, String date, Bitmap bitmap) {
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ID, id);
cv.put(CATEGORY, cateogry);
cv.put(STATUS, status);
cv.put(FEEDBACK, feedback);
cv.put(IMAGE, Utility.getBytes(bitmap));
cv.put(DATE, date);
db.insertWithOnConflict(INFR_TABLE, ID, cv,
SQLiteDatabase.CONFLICT_REPLACE);
}
try this and Update i think it should work ..
Instead of maintaining to two blocks of code you can maintain into a single block by using insertWithOnConflict.
public long insertWithOnConflict (String table, String nullColumnHack,
ContentValues initialValues, int conflictAlgorithm)
In the conflictAlgorithm you can specify what if an conflict occurs while inserting the record here you can specify CONFLICT_REPLACE
According to docs,
When a UNIQUE constraint violation occurs, the pre-existing rows that
are causing the constraint violation are removed prior to inserting or
updating the current row. Thus the insert or update always occurs. The
command continues executing normally. No error is returned.
so simply you can specify as
database.insertWithOnConflict(tablename, columnHack, values, SQLiteDatabase.CONFLICT_REPLACE);
it will inserts the record if it already exists otherwise it will update.
I want to update a field in my database, but the changes not saved to database.
I use The "update" method in database class but not work
Please
This is my Method
public void myupdatedb(int id,String value){
ContentValues cv = new ContentValues();
cv.put("fav", "1");
mydb.update("contents", cv, "id" + "=" + id , null);
}
and in the MainActivity:
db.myupdatedb(1,"1");
From Android documentation
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Try
mydb.update("contents", cv, "id = ?", new String[]{Integer.toString(id)});
I read about
ContactsContract.CommonDataKinds.GroupMembership, but I can't figure out what URI use to insert to.
I have prepared this method:
public static Uri addToGroup(ContentResolver resolver, long personId,
long groupId) {
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID, personId);
values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId);
return resolver.insert(uri, values);//URI is not known
}
Can someone tell me what URI to use in SDK 2.0+?
I found the resolution and I post it here:
public Uri addToGroup(long personId, long groupId) {
//remove if exists
this.removeFromGroup(personId, groupId);
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
personId);
values.put(
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
groupId);
values
.put(
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
return this.ctx.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
But I don't get something, why do I have to use RAW_CONTACT_ID and not CONTACT_ID, the later raises nullpointerexception.