Android uses listview control to operate SQLite database instance

In this example, firstly, we use sqliteopenhelper class to establish a database, write the methods of adding, deleting and querying, and connect listview through simplecursoradapter to realize the functions of database addition, query and long-click deletion.

First, let's understand what the sqliteopenhelper class is.

In order to operate SQLite database, Android provides sqlitedatabase class, which encapsulates operations such as insert, delete, update, query and executing SQL commands. At the same time, an auxiliary class sqliteopenhelper is provided for sqlitedatabase. It provides two important methods, namely:

Oncreate (sqlitedatabase dB): the database is generated when the user uses the software for the first time. Once the database exists, this method will not be called. The function is executed when the database is created for the first time. It will not be called when only the databasehelper pair (sqliteopenhelper type) is generated. Only when getreadabledatabase of the databasehelper object is called or getwritabledatabase is called, if the database is created for the first time, oncreate() function will be called.

Onupgrade (sqlitedatabase dB, int OldVersion, int vewversion): used to update the database table structure when upgrading the software, such as adding table and column fields.

If you implement these two methods, you can use its getwritabledatabase () and getreadabledatabase () to obtain the database (sqlitedatabase object).

If you need to upgrade the database table structure, you need to actively call onupgrade (sqlitedatabase dB, int OldVersion, int vewversion) and pass in a new version number.

The code for creating a new database is as follows:

Next, we create mainactivity:

Create a new activity to display the query results:

main_ Layout file for activity

Query result page layout file:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>