如何确保数据得到插入SQLite中只有一次?数据、SQLite

2023-09-08 09:31:24 作者:落雨笙璃

我的项目要求学生检索的主服务器(MySQL数据库)一类程序(附表),以自己的Andr​​oid设备(SQLite的)。

My Project requires a student to retrieve a Class routine(Schedule) from The main Server(MySQL database) to their android device(SQLite).

- >要求学生拿到常规形式在服务器上的Andr​​oid设备的负载,他们第一次运行该应用程序,然后获取即时更新程序的时间表/课程更新。它们应该能够查看在离线模式下的常规因此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.

我已经检索和分析使用PHP和JSON数据库中的数据。我需要插入SQLite中的日常细节只有一次。所以,做我必须做的数据检索,分析(从主服务器),并插入到SQLite的,在数据库的OnCreate 方式或有另一种方式来更有效地做到这一点。

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.

抱歉,如果这是一个愚蠢的问题,我是新来的SQLite和Android。

Sorry if this is a stupid question i'm new to SQLite and android.

推荐答案

我用的共享preferences插入数据只有一次,简单地使用这种方式:

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;
}
 
精彩推荐
图片推荐