The signature of the insert method of SQLiteDatabase is Long Insert (String Table, String Nullcolumnhack, Content Values Values), and the parameters of this insert method are described as follows:
Table: indicates the name of the table into which you want to insert data.
NullColumnHack: the column name of the data column that indicates the forced insertion of null values.
Value: data representing a row of records.
A row of records inserted by the insert method is stored by ContentValues, which is similar to a mapping. It provides put(String key, Xxx value) method for storing data and getAsXxx(String key) method for obtaining data.
For example, the following statement:
content values values = new content values();
Values.put ("name", "the Monkey King"):
Values.put ("age", 500);
//Returns the line number of the newly added record, which is an internal straight line regardless of the primary key id. If an error occurs, it will return-1.
long rowid = db . insert(" person _ INF ",null,values);
2. update the data by using the update method.
The symbol of the update method of SQLiteDatabase is update (string table, content values values, string where clause, string [] where args), and the parameters of the update method are described as follows:
Table: indicates the name of the table whose data is to be updated.
Values: indicates the data to be updated.
WhereClause: records that meet whereClause will be updated.
WhereArgs: used to pass the parameters of WhereArgs clause.
For example, if we want to update the name of everyone whose primary key is greater than 20 in the person_inf table, we can call the following method:
content values values = new content values();
//Store the updated name.
Values.put("name "," new name ");
int result = db . update(" person _ INF ",values," _ id & gt?" , new integer [] {20});
3. Use the delete method to delete records.
The sign of the delete method of SQLiteDatabase is delete (string table, string where clause, string [] where args), and the parameters of this deletion are described as follows:
Table: The name of the table that represents the data to be deleted.
Where clause: records that meet the where clause will be deleted.
WhereArgs: parameter used to pass in WhereArgs clause.
Delete all records in the person_inf table whose names start with Sun.
int result = db . delete(" person _ INF "," person_name like?" ,new String[]{ " Sun _ " });
4. Use the query method to query records.
The query method symbol of SQLiteDatabase is cursor query (Boolean distinct, string table, string [] columns, string selection, string [] selection args, string group by, string having, string order by, string limit), and the parameters of this query method are described as follows.
Distinct: Specifies whether to delete duplicate records.
Table: the name of the table where the query data is executed.
Columns: the name of the column to query.
Select: query condition clause.
SelectionArgs: parameter values used to pass in placeholders in the selection clause. The position of the value in the array must be consistent with the position of the placeholder in the statement, otherwise an exception will occur.
GroupBy: used to control grouping.
Having: used to filter packets.
OrderBy: used to sort records.
Limit: used for paging.
For example, query data in the person_inf table whose names begin with Sun.
cursor cursor = db . query(" person _ INF ",new String[]{"_id,name,age"}," name like?" ,new String []{ "Sun%"},null,null," personiddesc "," 5, 10 ");
Cursor.close ();