Android database tutorial

本文深入探讨了Android平台中SQLiteOpenHelper类中的onCreate()和onUpgrade()方法的语义。当数据库首次创建时,onCreate()方法被调用,用于创建已定义的表并执行其他代码。而当数据库版本升级时,onUpgrade()方法仅在当前运行版本的整数大于构造函数传递的版本时被调用。为了使这些方法运行,可能需要删除数据库文件并通过SQLite工具进行操作。

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.

http://www.vogella.de/articles/Android/article.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值