How to use SQLite developed by Android

SQLite is a lightweight small database. Although it is relatively small, its functions are relatively perfect. Some common database basic functions also have. It is used more in today's embedded system because it occupies very little system resources. The Android system is no exception. SQLite is also used. In this section, we will learn how to use the database to store data in Android, and complete simple operations such as new creation, update, query and deletion of SQLite.

Experiment Description:

When using SQLite database in Android, you need to use ADB to assist debugging. If you want to use ADB in CMD command line under windows, you must first configure the environment variable. Here is the configured user environment variable path: C: \ program files \ Android SDK \ platform tools;

After configuring the environment variables, enter ADB shell in CMD. Before entering the Linux shell environment, you need to open the Android simulator (this article is for the simulator, not the real machine). If the simulator is started and the error: device not found error prompt appears after entering the ADB shell command, you can kill the ADB process and restart it. Enter the following command in CMD:

  adb kill-server

  adb start-server

If you want to use SQLite in Android, you generally need to rewrite a class, which inherits a helper class sqliteopenhelper provided by Android to access the database. In this experiment, we create a new class named databasehelper under the SRC folder. This class inherits the sqliteopenhelper class, and the class inheriting the sqliteopenhelper class must have its own constructor. Therefore, according to Mr. Mars's code, we write three constructors in the program. The difference between these three functions is the number of parameters, The function with few parameters is because other parameters have been fixed, and they all explicitly or implicitly call the constructor of the parent class. The following is the constructor of sqliteopenhelper.

  public sqliteOpenHelper (Context context, String name, sqliteDatabase.CursorFactory factory,int version)

The first parameter is the class itself; The second parameter is the name of the database; The third parameter is used to set the cursor object, which is generally set to null here; Parameter 4 is the version number of the database.

  public void execsql (String sql)

This function is a function in the sqlitedatabase class. Its function is to execute an SQL statement command. The content of this statement is the parameters of the function. Therefore, SQL parameters need to conform to SQL syntax rules.

  public ContentValues ()

Contentvalues is a class used to store data in the database. It also uses key value pairs to store data, which is somewhat similar to content and bundle. The constructor is to create an empty dataset of the default size.

Experimental steps and results:

Interface design:

The interface design of the program is relatively simple. Because six functions need to be completed, there are only six buttons in the interface, and each button corresponds to a function. The functions of these buttons are to create a new database, update the version of the database, insert records into the database, update records in the database, query records in the database and delete records in the database. The interface effect is as follows:

Establish database:

Run the simulator and click the create SQLite database button to create a database named "tornadomeet" with the version number of 1. At this time, you can see that the program terminal displays the words "create a SQLite database". Enter the following command in CMD: ADB shell; cd data; cd data; ls; cd com.example.sqlite_ test; ls; Command, where com.example.split is the package name of the program. After entering the last LS command, you can see that there are two folders cache and lib. Then, after clicking, continue to view the LS command, and there is an additional database folder. After entering the folder with the command CD Database, LS finds a database in it. The screenshot is as follows:

Continue to enter SQLite3 tornadomeet.db on the CMD command line; (where tornadomet is the name of the newly created database after clicking Create database.) enter. Schema; (note that there is a dot in front) is displayed as follows:

You can see that there are two tables in this database, and you can see the SQLite statements created by these two tables. The first one represents the built-in Android, and the second one represents the new one.

Continue to enter the command select * from user1; (note that it is a real SQLite statement at this time, so it should end with a semicolon in the command line) query the table user1 in your own database and find that there is nothing in it.

Update database version:

In this step, after pressing the update SQLite database button, we create a new database in the listener function. The database name is "tornadomeet", but its version number is set to 2. Because the version number has changed, the program will automatically call the onupgrade method of the subclass of sqliteopenhelper. The program only outputs a statement in this method.

Database insert:

Click the insert button of the simulator, enter the database corresponding to SQLite, and enter select * from user1; After the command (with semicolon), you can see the following display: 1 | tornado, indicating that our database has successfully inserted a record, and the content of the record is statically given in the program.

Update database content:

The update operation is equivalent to executing the update statement of SQL. The syntax is: update table_ name SET ###col =###1 WHERE ###col = ###2

This program changes the name of a record inserted above from tornado to tornadomeet, so when you press the update button and re use select * from user1 in CMD, you can see that the record has become 1 | tornadomeet.

Query operation:

The query method in sqliteopenhelper is used for the query operation. The query here refers to the query according to the column name. In the actual query process, there is a cursor, which points to the header at the beginning and moves down again and again until the query conditions are met, and then the query is terminated.

Delete operation:

The deletion operation is similar to the previous one. Use the delete method in sqliteopenhelper to set a value according to the specified column name, and then delete the record.

The following are the results of ADB background debugging after clicking the new SQLite database button, insert record button, update record button, insert record button again, and delete button:

It can be seen that the deletion operation is also successful.

Codes and notes of the main parts of the experiment (the appendix has a link to download the experimental engineering code):

Appendix:

Experimental engineering code download

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