Android implements SQLite Add, update, and delete rows _android

Source: Internet
Author: User
Tags sqlite

This article illustrates the way Android implements SQLite Add, update, and delete rows. Share to everyone for your reference, specific as follows:

The Sqlitedatabase class exposes specific methods, such as inserts, deletes, and updates, which wrap the SQL statements required to perform these actions. However, the Execsql method allows you to execute any valid SQL statements on a database table that you want to perform manually.

At any time, if you modify the value of the underlying database, you should call the cursor Refreshquery method that any browses on the current table.

Insert New row

To create a new row, construct a Contentvalues object and use its put method to provide a value for each column. Inserting a new row by invoking the Insert method on the target database object and passing the Contentvalues object into the method-requires the name of the table-as shown in the following fragment:

Create a new row of values to insert.
Contentvalues newvalues = new Contentvalues ();
Assign values for each row.
Newvalues.put (column_name, newvalue);
[ ... Repeat for each column ...]
Insert the row into your table
Mydatabase.insert (database_table, NULL, newvalues);

Update rows

Update rows also need to be implemented through Contentvalues.

Create a new Contentvalues object and use the Put method to specify a new value for each column you want to update. Invokes the Update method of the database object, passing in the table name and the updated Contentvalues object, a where language that indicates which rows need to be updated.

The updated processing is demonstrated in the following fragment:

Define the updated row content.
Contentvalues updatedvalues = new Contentvalues ();
Assign values for each row.
Updatedvalues.put (column_name, newvalue);
[ ... Repeat for each column ...]
String where = key_id + "=" + rowId;
Update the row with the specified index with the new values.
Mydatabase.update (database_table, updatedvalues, where, null);

Delete Row

To delete a row, you can simply call the Delete method on the database object, specifying the table name and a where statement to indicate which rows you want to delete, as shown in the following code:

Mydatabase.delete (database_table, key_id + "=" + rowId, NULL);

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

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.