Example of Android using SQLite database
1、 Introduction
SQLite database is a lightweight DBMS (database management system). SQLite uses a single file to store data, and the Android standard library includes SQLite library and some Java auxiliary classes. Main features: lightweight, single file, cross platform, open source.
2、 Use of SQLite database in Android
1. Create SQLite database
After executing this statement, you can see the database file just created under / data / data / package name / in the ADB shell
In Android, use the static method openorcreatedatabase (string path, sqlitedatabase. Cursorfactory factory factory) of sqlitedatabase to open or create a database.
It will automatically detect whether the database exists. If it exists, it will be opened. Otherwise, it will create a database; If the creation is successful, an sqlitedatabase object will be returned. If it fails, a FileNotFoundException exception will be thrown.
In addition to the above methods, Android also provides sqliteopenhelper to create a database. First, inherit sqliteopenhelper and override oncreate and onupgrade methods and construction methods
Then call when you need to create a database
The database created with sqliteopenhelper is saved in the / data / data / package name / databases / directory. You can see the created database file through the ADB shell
2. Create a data table in the database
After the database is created, create a table to save data. The table is stored in the database. Sample code entry
The SQLite data type is attached here
Null: this value can be null
Varchar (n): a string whose length is not fixed and whose maximum length is n, n cannot exceed 4000.
Char (n): a string with a fixed length of N, n cannot exceed 254.
Integer: the value is identified as an integer. It can be stored as 1,2,3,4,5,6,7,8... According to the size of the value
Real: all values are floating values and are stored as 8-byte IEEE floating tag serial number
Text: the value is a text string, which is stored using database encoding (tutf-8, utf-16be or utf-16-le)
Blob: the value is a blob data block, which is stored in the input data format. How to input is how to store, without changing the format.
Data: includes year, month and date
After executing the above statements, you can export the database file to the computer through Adm
Open the database file through the visualization tool to see the previously created table and the defined table fields
In addition to defining the method to create a table, you can also initialize the table structure by inheriting the oncreate method in the sqliteopenhelper class. In the following example, you can create a table by inheriting sqliteopenhelper.
3. Operation on the table (addition, deletion, modification and query of the data table)
Add: to add data to the table, you can use execsql (string SQL) provided in sqlitedatabase class to execute an SQL statement to insert data. However, androidsqlitedatabase class provides a simpler method to insert data
Execute the inserttable method and export the database again. You can see that a piece of data is added to the stutable table
Delete: Well, just inserted a piece of data...
Similarly, Android provides the delete method
Executing the above statement will delete the data just inserted in the stutable table.
Change:
After executing the update statement, export the database file and view it in the visualization tool
Query: in Android, querying data is implemented through cursor class. When sqlitedatabase. Query () method can be used, another method rawquery is used here
A demo using SQLite (hero management system)
Code of main operation database
SQLiteDemo
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.