Cursor for Android Database

Source: Internet
Author: User

Cursor
It should be no stranger. If you are engaged in. NET development, you can understand cursor as ado.net.
The data set in is equivalent to datareader. Today, I will talk about it separately to deepen my understanding of Using cursor in Android.

About cursor

When you understand and use Android cursor, you must first know several things about cursor:

  • Cursor is a set of rows.
  • Use movetofirst () to locate the first line.
  • You must know the name of each column.
  • You must know the Data Type of each column.
  • Cursor is a random data source.
  • All data is obtained by subscript.

Important cursor Methods
:

  • Close

    ()
    Close the cursor to release resources
  • Copystringtobuffer

    (INT columnindex, chararraybuffer
    Buffer)
    Retrieve the text of the requested column in the buffer and store it
  • Getcolumncount

    ()
    Returns the total number of all columns.
  • Getcolumnindex

    (String
    Columnname)
    Returns the name of the specified column. If no Column exists,-1 is returned.
  • Getcolumnindexorthrow

    (String
    Columnname)
    The specified column name is returned from scratch. If it does not exist, illegalargumentexception will be thrown.

    Exception.
  • Getcolumnname

    (INT columnindex)
    Returns the column name from the given index.
  • Getcolumnnames

    ()
    Returns the column name of a string array.
  • Getcount

    ()
    Returns the number of rows in cursor.
  • Movetofirst

    ()
    Move the cursor to the first line
  • Movetolast

    ()
    Move the cursor to the last line
  • Movetonext

    ()
    Move the cursor to the next row
  • Movetoposition

    (INT position)
    Move the cursor to an absolute position
  • Movetoprevious

    ()
    Move the cursor to the previous line

Let's take a look at a short piece of code:

 

If
(Cur. movetofirst ()
=
 
False
)
{

//
Empty cursorreturn
;
}

 

Access the subscript of cursor to obtain the data.

 

Int
Namecolumnindex
=
Cur. getcolumnindex (people. Name );
String name
=
Cur. getstring (namecolumnindex );

 

 

Now let's take a look at how to cyclically extract the data we need from cursor.

 

While
(Cur. movetonext ())
{

//
Cursor moved

//
Retrieve Data


}

 

 

When cur. movetonext () is false, the loop will jump out, that is, the cursor data loop is complete.

If you prefer to use a for loop instead of a while loop, you can use the following methods provided by Google:

  • Isbeforefirst

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

  • Isafterlast

    ()

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

    ()
    If true is returned, the game logo is disabled.

With the above method, data can be retrieved in this way

 

For
(Cur. movetofirst ();
!
Cur. isafterlast (); cur. movetonext ())
{

Int
Namecolumn
=
Cur. getcolumnindex (people. Name );

Int
Phonecolumn
=
Cur. getcolumnindex (people. number );
String name
=
Cur. getstring (namecolumn );
String phonenumber
=
Cur. getstring (phonecolumn );
}

 

Tip: data query in Android is implemented through the cursor class. When we use the sqlitedatabase. Query () method, we will get the cursor object, which points to each piece of data. The knowledge of ado.net may be better understood.

The cursor is located in the Android. database. cursor class. It can be seen that its design is based on the database service.

In addition, there are several sub-classes known:

  • Abstractcursor
  • Abstract1_wedcursor
  • Crossprocesscursor
  • Cursorwrapper
  • Matrixcursor
  • Mergecursor
  • Mockcursor
  • Sqlitecursor

For detailed usage and explanation, refer to the API.

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.