In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the SQLite technology in Android development? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Google provides SQLite for the larger data processing of Andriod. It is excellent in data storage, management, maintenance and other aspects, and its function is also very powerful. SQLite has the following characteristics:
1. Lightweight. To use SQLite, you only need to bring a dynamic library to enjoy all its functions, and the size of that dynamic library wants to be small.
two。 Independence. The core engine of the SQLite database does not rely on third-party software, nor does it require so-called "installation".
3. Isolation. All the information in the SQLite database (such as tables, views, triggers, etc.) is contained in a folder, which is easy to manage and maintain.
4. Cross-platform. SQLite currently supports most operating systems, not even computer operating systems, but also can run on many mobile phone systems, such as Android.
5. Multilingual interface. SQLite database supports multilingual programming interfaces.
6. Security. SQLite database implements independent transactions through monopolization and shared locks at the database level. This means that multiple processes can read data from the same database at the same time, but only one can write data.
Use of SQLite in Android
First create the database class:
View sourceprint?public class DatabaseHelper extends SQLiteOpenHelper {private static final String DB_NAME = "mydata.db"; / / Database name private static final int version = 1; / / Database version public DatabaseHelper (Context context) {super (context, DB_NAME, null, version); / / TODO Auto-generated constructor stub} @ Override public void onCreate (SQLiteDatabase db) {String sql = "create table user (username varchar (20) not null, password varchar (60) not null);"; db.execSQL (sql) } @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {/ / TODO Auto-generated method stub}} SQLiteOpenHelper class introduction
SQLiteOpenHelper is a helper class of SQLiteDatabase that manages database creation and version updates. It is common to set up a class to inherit it and implement its onCreate and onUpgrade methods.
Method name method description:
The SQLiteOpenHelper (Context context,String name,SQLiteDatabase.CursorFactory factory,int version) constructor, which typically passes a parameter such as the name of the database to be created
Called when onCreate (SQLiteDatabase db) creates a database
Called when the version of onUpgrade (SQLiteDatabase db,int oldVersion, int newVersion) is updated
GetReadableDatabase () creates or opens a read-only database
GetWritableDatabase () creates or opens a read-write database
Let's introduce the method to be called
Create the database:
What's special here is that you create a database by calling the getReadableDatabase () method of the SQLiteOpenHelper class.
View sourceprint?1DatabaseHelper database = new DatabaseHelper (this); / / only use this 2SQLiteDatabase db = null; 3db = database.getReadalbeDatabase () when this code is placed in the Activity class.
The SQLiteDatabase class provides us with many methods, and the more common ones are as follows:
(return value) method name method description
(int) delete (String table,String whereClause,String [] whereArgs) convenient method to delete data rows
(long) insert (String table,String nullColumnHack,ContentValues values) A convenient way to add data rows
(int) update (String table, ContentValues values, String whereClause, String [] whereArgs) convenient method to update data rows
(void) execSQL (String sql) executes a SQL statement, which can be a select or other sql statement
(void) close () shut down the database
(Cursor) query (String table, String [] columns, String selection, String [] selectionArgs, String groupBy, String having, String orderBy, String limit) query the specified data table to return a dataset with a cursor.
(Cursor) rawQuery (String sql, String [] selectionArgs) runs a preset SQL statement that returns a dataset with a cursor (the difference from the above statement * * is to prevent SQL injection)
The addition, deletion, modification and inspection of data can be realized in two ways.
Addition of data
1. Use the insert method
View sourceprint?1ContentValues cv = new ContentValues (); / / instantiate a ContentValues to load the data to be inserted cv.put ("username", "Jack Johnson"); / / add the user name 2cv.put ("password", "iLovePopMusic"); / / add the password 3db.insert ("user", null,cv); / / perform the insert operation
two。 Use execSQL to implement
View sourceprint?1String sql = "insert into user (username,password) values ('Jack Johnson','iLovePopMuisc'); / / insert SQL statement 2db.execSQL (sql); / / execute SQL statement
There are also two ways to delete data.
View sourceprint?1String whereClause = "username=?"; / / deleted condition 2String [] whereArgs = {"Jack Johnson"}; / / deleted conditional parameter 3db.delete ("user", whereClause,whereArgs); / / delete / / use execSQL implementation view sourceprint?1String sql = "delete from user where username='Jack Johnson'"; / / delete operation SQL statement 2db.execSQL (sql); / / delete operation
Data modification is the same as above, there are still two ways
View sourceprint?1ContentValues cv = new ContentValues (); / / instantiate ContentValues 2cv.put ("password", "iHatePopMusic"); / / add fields and contents to be changed 3String whereClause = "username=?"; / / modify condition 4String [] whereArgs = {"Jack Johnson"}; / / modify condition parameter 5db.update ("user", cv,whereClause,whereArgs) / / implement modification / / use execSQL implementation view sourceprint?1String sql = "update [user] set password = 'iHatePopMusic' where username='Jack Johnson'"; / / modified SQL statement 2db.execSQL (sql); / / execute modification
Data query
Data query is more complex than the previous methods, because the query will have a lot of conditions.
Query through query:
Public Cursor query (String table, String [] columns, String selection, String [] selectionArgs, String groupBy, String having, String orderBy, String limit)
Description of each parameter:
Table: table name
Colums: array of column names
Selection: conditional clause, equivalent to where
SelectionArgs: parameter array of conditional statements
GroupBy: grouping
Having: grouping condition
OrderBy: sort class
Limit: limitations of paging queries
Cursor: return value, equivalent to the result set ResultSet
Many methods are also provided for Cursor.
Method name method description:
Total number of records of getCount ()
IsFirst () determines whether the record is *.
IsLast () determines whether to * a record.
MoveToFirst () moves to * records
MoveToLast () moves to * a record
Move (int offset) moves to the specified record
MoveToNext () moves to scare a record
MoveToPrevious () moves to the previous record
GetColumnIndex (String columnName) gets the int type value of the specified column index
Implementation code:
Cursor c = db.query ("user", null,null,null,null,null,null); / / query and get the cursor if (c.moveToFirst ()) {/ / to determine whether the cursor is empty for (int item0
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.