Add, update, and delete rows

Source: Internet
Author: User

The sqlitedatabase class exposes specific methods, such as insert, delete, and update. These methods encapsulate the SQL statements required to execute these actions. Even so, the execsql method allows you to execute any valid SQL statements on the database table, and these actions are what you want to manually execute.

 

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

 

Insert new row

 

To create a new row, construct a contentvalues object and use its put method to provide values for each column. The insert method is called on the target database object, And the contentvalues object is passed into the method to Insert a new row-the table name is required-as shown in the following snippet:

 

// 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 row

 

The updated row must also 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. Call the update method of the database object to pass in the table name and the updated contentvalues object. A where language is used to specify which rows need to be updated.

 

The update processing is demonstrated in the following snippet:

 

// 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, specify the table name and a where statement to specify the rows you want to delete, as shown in the following code:

 

Mydatabase. Delete (database_table, key_id + "=" + rowid, null );

 

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.