0

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 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

But what would you set the cursor equal too? Cursor = DbHelper; for example?
0

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

Where would I put this line in my DbAdapter code? I understand what is happening but I don't know how to implement it.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.