Reputation: 53
My Project requires a student to retrieve a Class routine(Schedule) from The main Server(MySQL database) to their android device(SQLite).
->Students are required to get the routine form the server an load on their Android devices on the first time they run the application and then get updates as the timetable of the routine / courses are updated. They should be able to view the routine in offline mode hence SQLite will have identical data as on the server.
I've already retrieved and parsed data from the database using PHP and JSON. I need to insert the routine details in SQLite only once. So do i have to do the data retrieval, parsing(from the main server) and insertion to SQLite, on the database OnCreate method or is there another way to do it more efficiently.
Sorry if this is a stupid question i'm new to SQLite and android.
Upvotes: 2
Views: 1686
Reputation: 1291
i use sharedpreferences to insert data only once , simply use it this way :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstTime()) {
// insert data
}
}
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}
Upvotes: 4
Reputation: 12753
First, make one method in sqlite database class for checking if your existing table is empty or not. and call that method on oncreate method of your starting activity. if your table is empty then insert records in it otherwise not insert..
public boolean checkDBIsNull(String table) {
Cursor cur = db.rawQuery("SELECT COUNT(*) FROM " + table + "", null);
if (cur != null) {
cur.moveToFirst();
System.out.println("record : " + cur.getInt(0));
if (cur.getInt(0) == 0) {
System.out.println("Table is Null");
cur.close();
return true;
}
cur.close();
} else {
System.out.println("Cursor is Null");
return true;
}
System.out.println("Table Not Null");
return false;
}
Upvotes: 2