ADB
ADB

Reputation: 567

Android: Insert the data into the local db only once when app launch

I have two queries:

  1. I want to insert the data from the web services into the local SQLite DB. I want to insert this data only once then the next time it will not loaded from the web services. I have tried using Static variable but that didn't worked.

    static int UpdateOneTime=0;

    if (UpdateOneTime==0) {

    getData();

    UpdateOneTime++;

    }

  2. If I want to reload the data again means when app start every time it will get the data from the server and insert into the DB by just deleting the previous data in the table. I have tried by just calling the onUpgrade(SqliteDatabase db, int oldversion,int newversion); But it didn't worked. So how to solve these two issues. Suggest me something.

Upvotes: 1

Views: 2380

Answers (3)

Vattic
Vattic

Reputation: 311

For point number one: You can follow the answer of Marco.

But if i were you, i would create a configuration table that has a default value of a flag and let's make it equal false. And in oncreate() method in the activity i will query for this value if it equals false, then i never load that data from the WS. And if it equal true, then it means that i loaded that data before, so i can use the local data that saved before in the DB.

In case of false, you will load that data from the WS and after make sure that you load it & insert it successfully, you have to update this flag to make its value equal true, to avoid reloading the data in the next time of opening the application.

I think you can use sharedPreference instead of the configuration table.


Point #2: You want each time the application delete the old data & reload & insert the data again: You will create your SQLite DB normally, then you can make a class that extends Application class, this class is the first class that will be loaded once the application is opened, it contains an onCreate() method, you can first delete all records, then reload all data again and insert it into your local SQLite DB.

Note: don't forget to add a tag in the androidMainfest.xml file to refer to your application class, its a must step.

Read more about application class examples over the internet. http://developer.android.com/reference/android/app/Application.html

Upvotes: 0

Marco Aurélio Deleu
Marco Aurélio Deleu

Reputation: 4367

For the 1., you should @override the function onCreate which will create your database and then you can execute some Queries there. This function will only be called when you request the database and the device notices that the database doesn't exist yet.

The 2nd. you don't get to call it manually, if I remember, instead, it is a function called when you upgrade the version of your database. This is managed through the construct of SQLiteOpenHelper class.

Note: Both onCreate and onUpgrade are functions called on the creation of the database and on the upgrade of the database structure, not data.

Upvotes: 1

Georgy Gobozov
Georgy Gobozov

Reputation: 13731

Don't keep inserted\not inserted value in static variable. Every time when application started value will have default value. After first data insert you can save some value at preferences and then check it every time when app starts

Upvotes: 1

Related Questions