Android-save data to the database (2)

Source: Internet
Author: User

Put data into the database

You can import the contentvalues object into the instert () method to insert data into the database:

// Gets the data repository in write mode
Sqlitedatabase DB = mdbhelper. getwritabledatabase ();
 
// Create a new map of values, where column names are the keys
Contentvalues values = new contentvalues ();
Values. Put (feedreadercontract. feedentry. column_name_entry_id, ID );
Values. Put (feedreadercontract. feedentry. column_name_title, title );
Values. Put (feedreadercontract. feedentry. column_name_content, content );
 
// Insert the new row, returning the primary key value of the new row
Long newrowid;
Newrowid = dB. insert (
Feedreadercontract. feedentry. table_name,
Feedreadercontract. feedentry. column_name_nullable,
Values );

The first parameter of the insert () method is the table name. The second parameter provides a column name in the framework. When the contentvalues value is null, the Framework inserts a null value into the table (if this parameter is "null ", if there is no value, the framework will not insert a row into the table.

 

Read data from the database

To read data from the database, you need to use the query () method. You need to pass in the selection conditions and columns you want to obtain data for this method. The query result is returned in the cursor object.

Sqlitedatabase DB
= Mdbhelper. getreadabledatabase ();
 
// DefineProjectionThat specifies which columns from the database
// You will actually use after this query.
String [] projection = {
Feedreadercontract. feedentry. _ id,
Feedreadercontract. feedentry. column_name_title,
Feedreadercontract. feedentry. column_name_updated,
...
};
 
// How you want the results sorted in the resulting cursor
String sortorder =
Feedreadercontract. feedentry. column_name_updated + "DESC ";
 
Cursor c = dB. Query (
Feedreadercontract. feedentry. table_name, // the table to query
Projection, // The columns to return
Selection, // The columns for the WHERE clause
Selectionargs, // The values for the WHERE clause
Null, // don't group the rows
Null, // don't filter by row groups
Sortorder // The sort order
);

 

Use the moving method of the cursor object to view a row of data in the cursor. You must call this method before reading data. Generally, you should start by calling the movetofirst () method, which places the location of the read data to the first entity in the result set. For each row, you can call the corresponding get method of the cursor object to read the column value, if the getstring () or getlong () method. For each get method, you must pass the index location of the expected column to it. You can call the getcolumnindex () or getcolumnindexorthrow () method to obtain the index of the column. For example:

Cursor. movetofirst ();
Long Itemid = cursor. getlong (
Cursor. getcolumnindexorthrow (feedreadercontract. feedentry. _ id)
);

 

Delete data from a database

To delete row data from a table, you must provide selection criteria for identifying rows. Data API provides a mechanism for creating selection conditions, which prevents SQL injection. This mechanism divides the selection conditions into selection conditions and selection parameters. The condition clause defines the columns to be viewed and allows you to use Composite Columns for filtering. A parameter is a value that is used to filter data by users bound to a condition. This will not cause the same processing as an SQL statement, so it avoids SQL injection.

// Define 'where' part of query.
String selection = feedreadercontract. feedentry. column_name_entry_id + "like? ";
// Specify arguments in placeholder order.
String [] selelectionargs = {string. valueof (rowid )};
// Issue SQL statement.
DB. Delete (table_name, selection, selectionargs );

 

Update Database

When you need to edit the database value, use the update () method.

When updating data, this method combines the content Value Syntax in the insert () method with the where syntax in the delete () method.

Sqlitedatabase DB
= Mdbhelper. getreadabledatabase ();
 
// New value for one column
Contentvalues values = new contentvalues ();
Values. Put (feedreadercontract. feedentry. column_name_title, title );
 
// Which row to update, based on the ID
String selection = feedreadercontract. feedentry. column_name_entry_id + "like? ";
String [] selelectionargs = {string. valueof (rowid )};
 
Int COUNT = dB. Update (
Feedreaderdbhelper. feedentry. table_name,
Values,
Selection,
Selectionargs );

 

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.