Android SQLite uses

Source: Internet
Author: User
Tags sqlite database

1. SQLite 1.1 SQLite Introduction

SQLite is a lightweight database, is an acid-compliant relational database management system, the Android system has been in the framework of the use of the user-friendly statements.

1.2 SQLite data type

SQLite uses dynamic data types, which are automatically judged based on the value of the deposit.
SQLite has the following five commonly used data types:

Storage class Describe
Null NULL value
INTEGER Signed integer stored in 1, 2, 3, 4, 6, or 8 bytes, depending on the size of the value
REAL Floating-point value, which is stored as a 8-byte IEEE floating point number
TEXT Text strings, stored using database encoding (UTF-8, utf-16be, or Utf-16le)
Blob BLOB data, which is stored entirely according to its input

For more information, refer to <sqlite data types >

2. Android SQLite

Using SQLite in Android involves the following three classes

    • Sqliteopenhelper: abstract class. We do this by inheriting the class, and then rewriting the database creation and updating methods. We can also get the DB instance through objects of that class, or close the database.
    • Sqlitedatabase: Database Access class. We can use this class of objects to do some additions and deletions to the database operation.
    • Cursor: Cursors. A bit similar to the resultset in JDBC, the result set. Can be simply understood as a pointer to a record in the database.
2.1 Sqliteopenhelper

Sqliteopenhelper is an abstract class used to manage the creation and versioning of databases

Common methods are:

sqlitedatabase getreadabledatabase ()   :  Get a readable database, return sqlitedatabase object       sqlitedatabase Getwritabledatabase ()   :  Get a writable database, return Sqlitedatabavoid onCreate (sqlitedatabase)          :  Create a table When the database is first built it is executed void onOpen (sqlitedatabase)            :  Opens the database when the database is opened and is executed void Onupgrade (sqlitedatabase,int,int):  execute void close () when the database needs to be updated                           :  Close the database

We implement a class (like DBHelper) to inherit Sqliteopenhelper, while implementing OnCreate and Onupgrade methods.
Then create the DBHelper instance, get the DB instance through Getreadabledatabase ()/getwritabledatabase (), and then manipulate the database

2.2 Sqlitedatabase

Sqlitedatabase is a database object that provides a rich approach

/* static method, open or CREATE DATABASE */
sqlitedatabase openorcreatedatabase (String path, Sqlitedatabase.cursorfactory factory);
/* Insert Record */

LongInsert (string table, String nullcolumnhack, contentvalues values);
/* Delete Record */
intDelete (string table, String Whereclause, string[] whereargs);
/* Query Record */
Cursor Query (boolean distinct, String table, string[] columns,
String selection, string[] Selectionargs, String groupBy,
String has, string-by-clause, string limit);
/* Update record */
intUpdate (string table, contentvalues values, String whereclause, string[] whereargs)
/* Execute SQL statement */
voidExecsql (String sql)

For more methods, please refer to some methods of <sqlitedatabase >

2.3 Cursor

The Cursor is a collection of records for each row, and you must know the name and data type of each column when you use it
Common methods are as follows:

/*close cursors, freeing resources*/voidClose ()/*retrieves the text of the requested column in the buffer, storing it*/voidCopystringtobuffer (intcolumnindex, Chararraybuffer buffer)/*returns the total number of all columns*/intgetColumnCount ()/*returns the name of the specified column, no return-1*/intGetcolumnindex (String columnName)/*returns the specified column name from zero, no throw illegalargumentexception exception*/intGetcolumnindexorthrow (String columnName)/*returns the column name from the given index*/String getColumnName (intcolumnindex)/*returns the column name of a string array*/string[] Getcolumnnames ()/*returns the number of rows in the cursor*/intGetCount ()/*move cursor to first line*/boolean movetofirst ()/*move the cursor to the last line*/boolean movetolast ()/*move the cursor to the next line*/boolean movetonext ()/*move the cursor to an absolute position*/Boolean movetoposition (intposition)/*move cursor to previous line*/boolean movetoprevious ()

Reference:
<sqlite Tutorials >
<android operation SQLite Basic usage >
<android data storage and access--the beginning of the SQLite database >
<android Database storage mode (i)----The basic operation of SQLite >

Android SQLite uses

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.