Detailed explanation of Android lightweight database SQLite

Database is the core of Android storage scheme. In andorid, SQLite is very lightweight, and the execution of SQL statements is even faster than mysql. Sqlitedatabase is one of the core classes of operating database in Android. Sqlitedatabase can be used to open and operate the database. However, in order to upgrade and use the database more conveniently, we often use the subclass of sqliteopenhelper to complete the operation of creating and opening the database. Sqliteopenhelper is an abstract class in which there are two methods that must be implemented:

Let's create a new class dbhelper extensions sqliteopenhelper

In this way, our dbhelper class is written. Let's implement a query operation.

The first step is activity_ main. Adding listview display control to XML

activity_ main. xml

Step 2: create a new XML layout file to use as layout resources for list items

user_ list_ cell. xml

Step 3: main activity

Execute it to see if our query is successful?

When operating SQLite database, you should understand that there are two methods to add, delete, modify and query the database. One is to directly execute SQL statements by using rawquery method, and the other is to operate by using the corresponding methods of sqlitedatabase class. Here is an example of the second one. For example, we want to insert data name = 11 age = 22

After the insertion is successful, annotate the insertion statement, remove the annotation from the query statement, and restart. You will find that the last item is added successfully.

In addition, for the cursor object obtained by querying records, you need to use methods such as movetofirst, movetonext, movtoposition (position) to move the pointer to the corresponding position to read the query results.

After execution, you can find that name and age are all obtained and displayed in toast and system Out. Isn't it interesting?

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