I am trying to increment a value stored in my SQLite database by a certain value, for example by 5, each time a new entry is created. I would have to check if there were any previous entrys and then display the previous row's value + 5, and if there were no previous row's insert a standard value of 20. I am very much stumped on what to do because this all depends on being able to check if there are any previous rows, and I just cannot find out how to do that. Any help is appreciated!
2 Answers
Cursor.get count() returns the number of rows that satisfy the conditions. Use it to know if there are any rows.
The cursor is obtained from SQLiteDatabase.query().
I recommend you to read http://developer.android.com/guide/topics/data/data-storage.html
1 Comment
user1576752
But what would you set the cursor equal too? Cursor = DbHelper; for example?
You can do a select on the DB first:
SELECT MAX(column) as cnt FROM table WHERE field = value
If a record is found you have the highest value.
Now you know what to do (insert/update) and with what value it should be saved.
2 Comments
user1576752
Where would I put this line in my DbAdapter code? I understand what is happening but I don't know how to implement it.
Tom
It really depends on how your app is being setup. You can do it in the Activity. Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, order)