SQLite database in Android

Source: Internet
Author: User

SQLite is a lightweight relational database. It is small in size and has no configuration. It can run directly in application processes and is very suitable for Embedded operating systems.

First, create an SQLite database.

Use a class to implement the abstract class SQLiteHelper,

public class PersonSqliteHelper extends SQLiteOpenHelper {

public PersonSqliteHelper(Context context) {super(context, "person.db", null, 1);}
There are four parameters: context, database name, factory, and version.

There are four parameters to be passed in. You only need to keep one.

public void onCreate(SQLiteDatabase db) {db.execSQL("create table person (id integer primary key autoincrement,name varchar(20),number varchar(20))");}
OnCreate is the method called when the database is created for the first time.

These databases have not yet been created.

PersonSqliteHelper p=new PersonSqliteHelper(this);p.getWritableDatabase();

In this way, a database is created.

The getReadableDatabase () and getWritableDatabase () methods are available.

The following describes how to add, delete, and query SQLite using some common operations.

Private PersonSqliteHelper helper; // complete the initialization of helper in the constructor public PersonDao (Context context) {helper = new PersonSqliteHelper (context );}


Add data


/*** Add a piece of data to the database * @ param name * @ param number phone number * @ return returns the id of the inserted data **/private boolean find (String name) {SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. query ("person", null, "name =? ", New String [] {name}, null); boolean result = cursor. moveToNext (); cursor. close (); db. close (); return result ;}

In the above Code, a container is created through ContentValues, then the data is added to the ContentValues container through the put () method, and finally distributed to the data table through the insert () method.

The returned value is the id of the newly created row.


Query data


/*** Query whether a record exists * @ param name * @ return true exist false not exist **/private boolean find (String name) {SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. query ("person", null, "name =? ", New String [] {name}, null); boolean result = cursor. moveToNext (); cursor. close (); db. close (); return result ;}



Modify data


/*** Modify a record * @ param name of the person whose information is to be modified * @ param newNumber new phone number * @ return returns the number of affected records **/private int updata (String name, string newNumber) {SQLiteDatabase db = helper. getWritableDatabase (); ContentValues values = new ContentValues (); values. put ("number", newNumber); int numbers = db. update ("person", values, "name =? ", New String [] {name}); db. close (); return numbers ;}


Delete data


/*** Delete a piece of data * @ param name **/private int del (String name) {SQLiteDatabase db = helper. getWritableDatabase (); int number = db. delete ("person", "name =? ", New String [] {name}); db. close (); return number ;}


Query data


/*** Return all data information **/public List
 
  
FindAll () {SQLiteDatabase db = helper. getReadableDatabase (); List
  
   
Persons = new ArrayList
   
    
(); Cursor cursor = db. query ("person", new String [] {"id", "name", "number"}, null, null); while (cursor. moveToNext () {int id = cursor. getInt (cursor. getColumnIndex ("id"); String name = cursor. getString (cursor. getColumnIndex ("name"); String number = cursor. getString (cursor. getColumnIndex ("number"); Person p = new Person (id, name, number); persons. add (p);} cursor. close (); db. close (); return persons ;}}
   
  
 

Transactions in the database

The so-called transaction processing is to ensure the integrity of the operation, all operations either succeed at the same time or fail at the same time.

In SQLite, the use of things is as follows:

1. Start a transaction through beginTransation.

2. Use setTransactionSuccessful () to set the success flag of a transaction.

3. If the second step is called, the transaction is successfully submitted and ended. Otherwise, the transaction is rolled back. The method is endTransaction ().

For example:

Db. beginTransaction (); try {... // other part of the statement body db. setTransactionSuccessful () ;}finally {db. endTransaction ();}




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.