Lazy Beard
Lazy Beard

Reputation: 303

ContentValues put value as sql-condition

When I try to put some condition value into ContentValues variable, there is inserting as String.

contentValues = new ContentValues();
contentValues.put("DLM", "julianday('now', 'localtime')");

After executing

count = db.update(TABLE_NAME, contentValues, selection, selectionArgs);

the field is updated but the value is incorrect. I need to have the numeric date in the field, not string.

Another problem if I need to update existing field with the calculated value:

UPDATE tbl SET field=field*2

When I put the value like

contentValues.put("field", "field*2");

It has put the value as String. How I can get the value I really needed?

Upvotes: 2

Views: 1379

Answers (2)

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

you need to enter date like:

Calendar cal=Calendar.getInstance();

ContentValues values = new ContentValues();
values.put("DLM", String.format("%1$te-%1$tB-%1$tY",cal));//this will insert date with format 2 July 2011

For updating values like fieldValue*2,

you need to fetch the older values from table and then you need to pass it like:

values.put("field_name",new_field_value);

Upvotes: 0

slkorolev
slkorolev

Reputation: 6001

I think instead of update() method you should use execSQL method which allows you completely control the query syntax. Also instead of calculating fields in query you might consider creating triggers.

Upvotes: 1

Related Questions