A simple way to manipulate databases inside sqlitedatabase

Source: Internet
Author: User

1. Insert a record using the Insert method
The Sqlitedatabase Insert method is signed with a long insert (String table,string nullcolumnhack,contentvalues values), and the parameters of the Insert method are described below:
Table: Represents the names of the tables for which you want to insert data.
Nullcolumnhack: Represents the column name of a data column that forcibly inserts a null value.
Values: Represents the data for a row of records.
The Insert method inserts a row of records stored using contentvalues, contentvalues similar to map, which provides a put (String key,xxx value) (where key is the column name of the data column) method for storing data, The Getasxxx (String key) method is used to fetch data.
For example, the following statement:
Contentvalues values=new contentvalues ();
Values.put ("name", "Monkey King"):
Values.put ("Age", 500);
Returns the line number of the newly added record, which is an internal straight, independent of the primary key ID, and an error is returned-1
Long Rowid=db.insert ("Person_inf", null,values);


2. Update data using the Update method
The Sqlitedatabase Update method signature is update (String table,contentvalues values,string whereclause,string[] whereargs), The parameters of this update method are described below:
Table: Represents the names of the tables for which you want to update data.
Values: Represents the data that you want to update.
Whereclause: The record that satisfies the Whereclause clause will be updated.
Whereargs: Used to pass parameters for the Whereargs clause.
For example, if we want to update the names of all people with primary keys greater than 20 in the Person_inf table, you can call the following methods:
Contentvalues values=new contentvalues ();
Store the updated name
Values.put ("Name", "new name");
int Result=db.update ("Person_inf", Values, "_ID>?", New integer[]{20});


3. Delete records using Delete method
Sqlitedatabase's Delete method signature is delete (String table,string whereclause,string[] whereargs), the deleted parameter is described as follows:
Table: Represents the names of the tables for which you want to delete data.
Whereclause: The record that satisfies the whereclause clause will be deleted.
Whereargs: Used to pass in parameters for the Whereargs clause.
Delete a record of all names in the Person_inf table beginning with the Sun
int Result=db.delete ("Person_inf", "Person_name like", New string[]{"Sun _"});


4. Querying records using the Query method
Sqlitedatabase the Query method signature is cursor query (boolean distinct,string table,string[] columns,string selection,string[] selectionargs,string groupby,string having,string orderby,string limit), the parameters of this query method are described below.
Distinct: Specifies whether to remove duplicate records.
Table: The names of the tables that execute the query data.
Columns: The name of the column to query.
Selection: Query condition clause.
Selectionargs: Used to pass in a parameter value for a placeholder in the selection clause, the position of the value in the array must match the position of the placeholder in the statement, or there will be an exception.
GroupBy: Used to control grouping.
Having: used to filter the grouping.
ORDER BY: Used to sort records.
Limit: Used for paging.


For example, query the data in the Person_inf table that begins with a person's name
Cursor cursor=db.query ("Person_inf", New string[]{"_id,name,age"}, "name like?", New String []{"Grandchild%"},null,null, " PersonID desc "," 5,10 ");
Cursor.close ();

A simple way to manipulate databases inside sqlitedatabase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.