0

i have database with only a counter....i wanted to know how to increment it.... i have seen this article Increase the value of a record in android/sqlite database but dont really understand it.... does someone have a code sample which might be useful?

i basically want to make a quotes application... in that say the user has seen 50 out of 100 quotes and exits the application... i want to restart from 50 next time the user starts up the application... is there any other way apart from databases to accomplish this task?

Community
  • 1
  • 1

3 Answers3

1

Check this out: http://developer.android.com/guide/topics/data/data-storage.html

You probably want SharedPreferences if you are only storing one key-value pair.

skaz
  • 21,962
  • 20
  • 69
  • 98
1

Well I don't think you want a database. A database contains tables which in turn contain rows and columns. That's not what you want. You just want to store one single value; the "page" number.

For that you should use the SharedPreferences:

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
p.edit().putInt(PAGE_PREFERENCE, pageNumber).commit();

Then when you start your activity, you can retrieve this value like this:

int page = p.getInt(PAGE_PREFERENCE, 0);

Now if you insist to use a database, you can increment a value like this:

db.execSQL("UPDATE tableX SET value = value + 1 WHERE key =" + key, null);

I hope this helps.

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • your right i didnt want to use databases ....but i was looking for preferences and how they worked but didnt really understand it....but this makes sense and it works perfectly...thanks a lot... :) – Nikhlesh Lakhmani Nov 09 '10 at 22:46
0

Not very efficient but if you know how to create a DB then just have your oncreate append a boolean to the DB each time its called. The _id will autoincrememnt and you can just query for _id.

You can also put the quotes in the DB and query for quotes that have not been read by sorting by the boolean. When the quote is output it then puts a boolean in the DQ by the quote so it will be skipped the next time. Once the end of the list is reached you can (in theory) clear the booleans and start over.

Opy
  • 2,119
  • 3
  • 18
  • 22