Chapter 5 data-centric-Data Access (5), Chapter 5 Access

Source: Internet
Author: User

Chapter 5 data-centric-Data Access (5), Chapter 5 Access
5.2.3 add, delete, and modify operations

The following describes how to add, delete, and modify an object. The following describes how to perform database operations by encapsulating these actions in a class DBHelper.

// Import omitted

Public class DBHelper {

 

Private static final String [] COLS = new String [] {"_ id", "name "};

Private SQLiteDatabase db;

Private final DBOpenHelper dbOpenHelper;

 

Public DBHelper (final Context context ){

This. dbOpenHelper = new DBOpenHelper (context );

EstablishDb ();

}

/**

* Get a writable SQLite database. If the database has not been created,

* The DBOpenHelper auxiliary class is responsible for establishing this database.

* If the database has been created, a writable database is directly returned.

*/

Private void establishDb (){

If (this. db = null ){

This. db = this. dbOpenHelper. getWritableDatabase ();

}

}

/**

* Shut down the database.

*/

Public void cleanup (){

If (this. db! = Null ){

This. db. close ();

This. db = null;

}

}

/**

* Insert a data entry

*/

Public void insert (String id, String name ){

ContentValues values = new ContentValues ();

Values. put ("_ id", id );

Values. put ("name", id );

This. db. insert (DBOpenHelper. TABLE_NAME, null, values );

Cleanup ();

}

/**

* Update a piece of data

*/

Public void update (String id, String name ){

ContentValues values = new ContentValues ();

Values. put ("_ id", id );

Values. put ("name", id );

This. db. update (DBOpenHelper. TABLE_NAME, values, "_ id =" + id, null );

Cleanup ();

}

/**

* Delete a piece of data

*/

Public void delete (final long id ){

This. db. delete (DBOpenHelper. TABLE_NAME, "_ id =" + id, null );

}

/**

* Delete all data

*/

Public void deleteAll (){

This. db. delete (DBOpenHelper. TABLE_NAME, null, null );

}

Private void query (){

// Obtain a writable database.

SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();

// Query the database

Cursor cur = db. query (DBOpenHelper. TABLE_NAME, COLS, null );

If (cur! = Null ){

For (cur. moveToFirst ();! Cur. isAfterLast (); cur. moveToNext ()){

Int idColumn = cur. getColumnIndex (COLS [0]);

Int nameColumn = cur. getColumnIndex (COLS [1]);

String id = cur. getString (idColumn );

String name = cur. getString (nameColumn );

}

Cur. close ();

}

}

}

---------------------------------------- It is difficult for programmers to make money. Therefore, they must learn financial management. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. We recommend investing in don't invest in security, which is almost impossible to transfer. It is very difficult to withdraw the website link in advance. Don't make it in white --------------------------------------------

The Code "Cursor cur = db. query (DBOpenHelper. TABLE_NAME, COLS, null, null);" puts the queried data into a Cursor. This Cursor encapsulates all the columns in TABLE_NAME of the data table. The query () method is quite useful. Here we will briefly talk about the parameters in the query.

The first parameter is the name of the table in the database. In our example, the table name is TABLE_NAME, that is, "myTableName ".

The second parameter is the column information that we want to return data. In this example, we want to obtain the id and name columns. We put the names of these two columns in the string array.

The third parameter is selection, which is equivalent to the where part of the SQL statement. If you want to return all the data, you can directly set it to null.

The fourth parameter is selectionArgs. In the selection section, you may use "?", The string defined in selectionArgs will replace "?" In selection "?".

The fifth parameter is groupBy, which defines whether the queried data is grouped. If it is null, it means no grouping is required.

The sixth parameter is having, which is equivalent to having in an SQL statement.

The seventh parameter is orderBy to describe whether the expected return value needs to be sorted. If it is set to null, it means no sorting is required.

Integernum = cur. getCount (); you can use the getCount () method to obtain the number of data in cursor.

Next we will introduce some common methods of the Cursor class.

5.2.4 Cursor operations-Use Cursor

In databases, cursor is a very important concept. A cursor provides a flexible means to operate the data retrieved from a table. In essence, A cursor is actually a mechanism that can extract a record from a result set that contains multiple data records.

In the example in the previous section, we see the Cursor, which is the Cursor. We can simply think of Cursor as an object pointing to a row of data in the database. In Android, the query database is implemented through the Cursor class. When we use the SQLiteDatabase. query () method, we will get a Cursor object, which points to each piece of data. It provides many query methods, as shown in Table 5-2.

Method

Return Value

Description

Close ()

Void

Close the cursor to release resources

CopyStringToBuffer (int columnIndex, CharArrayBuffer buffer)

Void

Retrieve the text of the requested column in the buffer and store it

GetColumnCount ()

Int

Returns the total number of all columns.

GetColumnIndex (String columnName)

Int

Returns the name of the specified column. If no Column exists,-1 is returned.

GetColumnIndexOrThrow (String columnName)

Int

The specified column name is returned from scratch. If the column name does not exist, an IllegalArgumentException exception is thrown.

GetColumnName (int columnIndex)

String

Returns the column name from the given index.

GetColumnNames ()

String []

Returns the column name of a string array.

GetCount ()

Int

Returns the number of rows in Cursor.

MoveToFirst ()

Boolean

Move the cursor to the first line

MoveToLast ()

Boolean

Move the cursor to the last line

MoveToNext ()

Boolean

Move the cursor to the next row

MoveToPosition (int position)

Boolean

Move the cursor to an absolute position

MoveToPrevious ()

Boolean

Move the cursor to the previous line

IsBeforeFirst ()

Boolean

Returns whether the cursor points to the position of the first row.

IsAfterLast ()

Boolean

Returns whether the cursor points to the position of the last row.

IsClosed ()

Boolean

If true is returned, the cursor is closed.

Table 5-2 Cursor Method

 

The previous code example contains a simple example of Using Cursor.

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.