http://www.vogella.de/articles/AndroidSQLite/article.html
Semantics of SQLiteOpenHelper.onCreate() and onUpgrade() (original URL: http://alexlod.com/2011/07/26/android-sqliteopenhelper-oncreate-and-onupgrade-semantics/ )
Overall I've been insanely impressed with Android's documentation. I'd even go so far to say that the Android platform has the best documentation of any platform or technology I've ever learned, including Django and Python.
However, I couldn't find much on the semantics of the onCreate() and onUpgrade() methods in android.database.sqlite.SQLiteOpenHelper, which is a class that helps you open and upgrade a SQLite database. I'll describe below how these methods are called when databases are created and upgraded.
Database Creation
When the SQLite database is constructed for the first time, as you'd expect the onCreate() method is called, creating the tables you've defined and executing any other code you've written. However, this method will only be called if the SQLite file is missing in your app's data directory (/data/data/your.apps.classpath/databases). This method will not be called if you've changed your code and relaunched in the emulator. If you want onCreate() to run you need to use adb to delete the SQLite database file. The sqlite3 tool description has more context here.
Database Upgrading
The constructor of your implementation of SQLiteOpenHelper should call the super constructor, passing along the database name and version. The onUpgrade() method will only be called when the version integer is larger than the current version running in the emulator. If you want the onUpgrade() method to be called, you need to increment the version number in your code.
The complete tutorial for Gingerbread.
本文深入探讨了Android平台中SQLiteOpenHelper类中的onCreate()和onUpgrade()方法的语义。当数据库首次创建时,onCreate()方法被调用,用于创建已定义的表并执行其他代码。而当数据库版本升级时,onUpgrade()方法仅在当前运行版本的整数大于构造函数传递的版本时被调用。为了使这些方法运行,可能需要删除数据库文件并通过SQLite工具进行操作。

1460

被折叠的 条评论
为什么被折叠?



