android== "Data Article =" SQLite

Source: Internet
Author: User

About SQLite:

SQLite is an open source embedded database engine written in C language. It supports most of the SQL92 standards and can be run on all major operating systems.

-support for databases up to 2TB in size

--In the form of a single file

--stored on disk in the form of a B-TREE data structure

Main features include:

1. Lightweight a dynamic library single file

2. Independence is not dependent, no installation required

3. All of the isolation in one folder

4. Cross-platform support for many operating systems

5. Multi-lingual interface supports many programming languages

6. Security Services

About security issues with transactions:

--independent transaction processing through exclusive and shared locks on the database.

--Multiple processes can read data from the same database at the same time, but only one can write data.

Data types for SQLite:

--sqlite supports Null,integer,real,text and BLOB data types

--in turn: null value, integer value, floating point value, String value, binary object

Dynamic Data type (weak reference)

-- when a value is inserted into the database, SQLite checks its type, and if the type does not match the associated column, SQLite attempts to convert the value to the type of the column, and if it cannot, the value will be stored as its own type.


How to use SQLite in Android:

--sqlitedatabase

--sqliteopenhelper


public class Mainactivity extends Activity {
private static final String TABLENAME = "Stutb";


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Sqlitedatabase db = Openorcreatedatabase ("Stu.db", mode_private, NULL);
Db.execsql ("CREATE table if not exists STUTB (_id integer primary key autoincrement, name text not null,sex text NOT NULL, Age integer NOT NULL) ");

Contentvalues values = new Contentvalues ();
Values.put ("name", "Zhang Three");
Values.put ("Sex", "male");
Values.put ("Age", 18);
Long rowId = Db.insert ("Stutb", null, values);
Values.clear ();

Values.put ("name", "Zhang San Feng");
Values.put ("Sex", "male");
Values.put ("Age", 108);
Db.insert ("Stutb", null, values);
Values.clear ();

Values.put ("name", "Cheung San Fung");
Values.put ("Sex", "male");
Values.put ("Age", 108);
Db.insert ("Stutb", null, values);
Values.clear ();

Values.put ("name", "Zhang San Feng");
Values.put ("Sex", "male");
Values.put ("Age", 108);
Db.insert ("Stutb", null, values);
Values.clear ();

Values.put ("Sex", "female");

Db.update ("Stutb", Values, "_ID>?", New string[]{"3"});//Change the sex of all id>3 into female
Db.delete ("Stutb", "name like?", new string[]{"% abundance"});//delete all names with abundant people
Cursor C = db.query ("Stutb", NULL, "_ID>?", New string[]{"0"}, NULL, NULL, "name");
if (c! = null) {
string[] columns = C.getcolumnnames ();
while (C.movetonext ()) {
for (String columnname:columns) {
LOG.I ("Info", C.getstring (C.getcolumnindex (ColumnName)));
}
}
C.close ();
}

}


Sqliteopenhelper:

>sqlitedatabase help class for managing database creation and version updates

> generally builds a class to inherit it, and overrides the OnCreate () and Onupgrade () methods

> method Description:

* OnCreate (Sqlitedatabase db) called when creating a database

*onupgrade (sqlitedatabase db,int oldversion,int newversion) is called when the version is updated

*getreadabledatabase () Create or open a read-only database

*getwritabledatabase () Create or open a read-write database


code example:

public class Dbopenhelper extends sqliteopenhelper{


Public Dbopenhelper (Context context, String name) {
Super (context, name, NULL, 1);
TODO auto-generated Constructor stub
}

Public Dbopenhelper (Context context, String name, Cursorfactory factory,
int version) {
Super (context, name, Factory, version);
TODO auto-generated Constructor stub
}


The first time you create a database call can build a library, the operation of building a table
@Override
public void OnCreate (Sqlitedatabase db) {
TODO auto-generated Method Stub
Db.execsql ("CREATE table if not exists STUTB (_id integer primary key autoincrement,name text not null,sex text not Null,ag e integer NOT null) ");
Db.execsql ("INSERT into STUTB (name, sex, age) VALUES (' Zhang San ', ' female ', 18)");
}



When the version of the database changes, it is automatically executed
@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
TODO auto-generated Method Stub

}


}

========================

public class Mainactivity extends Activity {


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Dbopenhelper helper = new Dbopenhelper (mainactivity.this, "stu.db");
Sqlitedatabase db = Helper.getwritabledatabase ();

Cursor C = db.rawquery ("Select*from Stutb", null);
if (c!= null) {
string[] cols = C.getcolumnnames ();
while (C.movetonext ()) {
for (String Columnname:cols) {
LOG.I ("info", columnname+ ":" +c.getstring (C.getcolumnindex (ColumnName)));
}

}
C.close ();
}
Db.close ();
}


}


android== "Data Article =" SQLite

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.