Android-sqlitedatabase and SQLite Database

Source: Internet
Author: User

Recently I am looking at the source code of the Android platform. Next I will summarize the relationship between sqlitedatabase and its underlying sqllite database. How does the upper-layer sqlitedatabase call the underlying SQLite database operation.

 

Sqlitedatabase is a class used by the android framework layer to encapsulate sqllite database operations. Apps can directly use sqlitedatabase to use databases on the Android platform. Developers can use sqlitedatabase to implement a contentprovider.

SQLite database is a lightweight relational database written in C code. Used by the Android system.

 

The following two operations are used to summarize the call between sqlitedatabase and SQLite database.

The first one is sqlitedatabase: Query ()

The second is sqlitedatabase: insert ().

 

1. Query

Sqlitecursor lcursor = sqlitedatabase: Query (sqlstate );

While (lcursor-> movetonext ())

{

Stringlstring = lcursor-> getstring ();

}

 

Sqlitedatabase: Query () returns a sqlitecursor. sqlitecursor is a cursor class specifically written to sqlitedatabase. It is used to move the cursor over the query results, allowing the app to retrieve data from sqlitedatabase.

Here we need to mention a class called cursorwindow, which acts as a buffer to store the results returned by query. Sqlitecursor's walk is also on the cursorwindow class.

 

Sqlitecursor inherits from abstractwindowcursor (AWC) and AWC inherits from cursor. AWC has a member variable cursorwindow, so the sqlitecursor class has cursorwindow.

 

If sqlitecursor wants to go upstream of cursorwindow, it first needs to fill in cursorwindow. sqlitecursor implements a fillwindow () interface to obtain query results from the database, then write the query result to cursorwindow.

There is also the fillwindow () interface. When querying the database, it does not return all the data, but obtains some data and writes it to cursorwindow, when the sqlitecursor game reaches the last row of cursorwindow, it will call fillwindow () again to obtain data again.

 

Workflow of movetonext:

Sqlitecursor: movetonext ()-à abstractcursor: movetonext (),-à sqlitecursor: onmove ()

When the cursor moves to the last row of cursorwindow, you need to call sqlitecursor: fillwindow () to fill in the cursorwindow, à sqlitecursor: fillwindow () to call sqlitequery: fillwindow () ------------------------- à call the native_fill_window () interface through the JNI interface, and then call the C interface sqlte3_step () of SQLite database in native_fill_window () to execute the SQL statement, then, enter the returned data results in cursorwindow.

Workflow of getstring:

Sqlitecursor: getstring ()-à AWC: getstring () à call cursorwindow: getstring (). Finally, cursorwindow still uses the JNI interface getstring_native () obtain data from the cursorwindow class written in C ++.

2. insert ()

Sqldatabase: insert () operations are relatively simple. It does not involve so many classes as query (). This interface is similar to workflow for delete, update, and other operations, and is relatively direct.

Generally, workflow is as follows:

1. In insert (), a sqlitestatement object is generated using the parameters passed in by insert (), representing an SQL statement,

2. Call sqlitestatement: Execute () to execute the SQL statement.

3. Call the native_execute () JNI interface in execute () for execution.

4. In native_execute () (android_database_sqlitestatement.cpp,

5. Call err = sqlite3_step (statement) to execute the SQL statement.

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.