简体   繁体   English

SQLite-Java-onCreate和onUpgrade-Android

[英]SQLite - Java - onCreate and onUpgrade - Android

Hello i have created my first application which collect some basic information in its local private database using my own class which inherits the SQLiteOpenHelper 您好,我创建了我的第一个应用程序,它使用我自己的继承SQLiteOpenHelper的类在其本地私有数据库中收集了一些基本信息。

I have noticed that when inheriting i have to implement some of the methods which are : 我注意到,在继承时,我必须实现一些方法:

  @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

what i have noticed is : in many tutorials i have seen people were using something like this : 我注意到的是:在许多教程中,我看到人们正在使用类似这样的东西:

 @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE table_name (parameters  here...)");
    }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS table_name");
    onCreate(db);
}

Question

i thought the code in the above approach does this: whenever the user restarts the app , it should delete the entire table and recreate it , but it doesnot , so whats the goal of it ? 我认为上述方法中的代码可以做到这一点:每当用户重新启动应用程序时,它都应删除整个表并重新创建它,但事实并非如此,那么它的目标是什么? i have just tested it on my phone , and basicaly i close the application and kill the proccess of it , then reopen it and the table is still there with the previous content , its not my goal , sure i want to keep me database between all the sessions but i just dont get this sample code... 我刚刚在手机上对其进行了测试,并且基本上我关闭了该应用程序并杀死了该进程,然后重新打开它,并且该表仍然与先前的内容一起存在,这不是我的目标,请确保我要保留我之间的数据库会议,但我只是没有得到此示例代码...

in which order are there methods called? 按什么顺序调用方法?

i thought the first is constructor and then onCreate and then onUpgrade but it doesnot delete the table so it means onUpgrade is not getting called? 我以为第一个是构造函数,然后是onCreate然后是onUpgrade,但它不会删除表,所以这意味着不会调用onUpgrade?

public void onUpgrade() is NOT same as onResume() . public void onUpgrade()onResume() onUpgrade() will be called only in case where user updates their app with newer version. 仅当用户使用较新版本更新其应用程序时,才会调用onUpgrade() Restart --> calls either onCreate() or onResume() . 重新启动->调用onCreate()onResume()

For example: 例如:

When you created app first time you have table EMPLOYEE with FIRSTNAME and LASTNAME 首次创建应用程序时,您拥有带有FIRSTNAME和LASTNAME的表EMPLOYEE

But in version2 of your app (which would be published at later date) you add new column MIDDLENAME to this table, then onUpgrade() call will be used (for user who are upgrading to new version). 但是,在应用程序的version2(将在以后发布)中,您将新列MIDDLENAME添加到此表中,然后将使用onUpgrade()调用(对于升级到新版本的用户)。

Based on SQLiteOpenHelper documentation : 基于SQLiteOpenHelper文档

Called when the database needs to be upgraded. 在需要升级数据库时调用。 The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version. 实现应使用此方法删除表,添加表或执行其他任何升级到新架构版本所需的操作。

onCreate() is called when the database is to be created. 创建数据库时将调用onCreate()

onUpgrade() is called when you increase the database version number. 当您增加数据库版本号时,将调用onUpgrade() In this case, you'll delete everything in the table and start over. 在这种情况下,您将删除表中的所有内容并重新开始。

You'll have to drop the tables manually every time the app resumes if you want to clear the database like this. 如果要清除数据库,则每次应用恢复时都必须手动删除表。 Then call onCreate() again to rebuild it. 然后再次调用onCreate()进行重建。

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

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