Analysis of sqliteopenhelper usage in Android Development

This article illustrates the usage of sqliteopenhelper in Android development. Share with you for your reference. The specific analysis is as follows:

If you directly use openorcreatedatabase (name, mode, factory) in activity in Android development, there will be a series of follow-up problems. For example, database upgrade and update.

It is best to use its encapsulated version: sqliteopenhelper

The main work of inheriting and extending sqliteopenhelper class is to override the following two methods.

Oncreate (sqlitedatabase dB): this method is executed when the database is created for the first time. Generally, initialization operations such as creating tables are executed in this method.

Onupgrade (sqlitedatabse DV, int OldVersion, int new version): this method will be called when the incoming version number is different from the current version number when the database is opened.

In addition to the above two methods that must be implemented, you can optionally implement the onopen method, which will be called every time you open the database.

Rewrite these functions in the business, and then get the database you want to operate through the helper's getwritabledatabase and getreadabledatabase. Just do it again.

In addition, to judge whether a table exists in SQLite, you can use the following methods:

I hope this article will be helpful to your Android program design.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>