简体   繁体   English

通过检查Android SDK中的数据库进行首次设置

[英]First time setup by checking the database in Android SDK

My application will show a first time setup screen when the user opens it for the first time. 当用户第一次打开它时,我的应用程序将显示一个第一次设置屏幕。 I need to know when it is the first time the application is executed and my question is: 我需要知道何时是第一次执行该应用程序,我的问题是:

Should I do this by checking and changing a flag setting in SharedPreferences or is it more secure to check if the database is empty (I use SQLiteOpenHelper )? 我应该通过检查和更改SharedPreferences的标志设置来执行此操作,还是检查数据库是否为空(我使用SQLiteOpenHelper )更安全? In the last case, how can I check if the database is empty? 在最后一种情况下,如何检查数据库是否为空?

SharedPreferences will most likely be quicker, but if you want to check for an empty database: SharedPreferences最有可能会更快,但是如果您要检查数据库是否为空,请执行以下操作:

Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM TABLE_NAME", null);
if (cursor != null){
    cursor.moveToFirst();
    if (cursor.getInt(0) == 0) {
      // It is Empty 
    }
}

where TABLE_NAME is the name of a table (that should be populated) in your database. 其中TABLE_NAME是数据库中表的名称(应填充)。 The only concern I have with the database is if you end up changing what and when something is populated, potentially resulting in the app failing to know the correct time to show the set-up screen - a single boolean SharedPreferences flag might be simpler. 我对数据库的唯一担心是,如果您最终更改了什么内容以及何时填充了某些内容,则可能导致应用程序不知道正确的时间来显示设置屏幕-单个布尔型SharedPreferences标志可能更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM