Android sqlitedatebase operation

Source: Internet
Author: User

Sqlitedatabase

 

[Function]

Sqlitedatabase is about database operations that can be used for insert Delete update query and other operations. Unfortunately, the following are some shortcomings:

1. It does not support creating databases

2. It does not support version updates or does not know how to do this because of the differences in specific data.

 

In view of the above defects, there is a helper class that can complete the above functions: sqliteopenhelper

 

 

[Code]

1. Define sqliteopenhelper and complete the update creation Function

Public class dbhelper extends sqliteopenhelper {

Public static final string tb_name = "mycountry ";
Public static final string id = "_ id ";
Public static final string Country = "country ";
Public static final string code = "code ";


Public dbhelper (context, string name,
Cursorfactory factory, int version ){
Super (context, name, factory, version );
}

Public void oncreate (sqlitedatabase dB ){

Db.exe csql ("create table if not exists"
+ Tb_name + "("
+ ID + "integer primary key ,"
+ Country + "varchar ,"
+ Code + "integer )");
}

Public void onupgrade (sqlitedatabase dB,
Int oldversion, int newversion ){

Db.exe csql ("Drop table if exists" + tb_name );
Oncreate (db );
}
}

2. Obtain the sqlitedatabase instance from sqliteopenhelper

Dbhelper helper = new dbhelper (this, db_name, null, version );

Sqlitedatabase DB = helper. getwritabledatabase ();

3. sqlitedatabase operations:

* Insert data:

Contentvalues values = new contentvalues ();
Values. Put (dbhelper. Country, "China ");
Values. Put (dbhelper. Code, 86 );
DB. insert (dbhelper. tb_name, dbhelper. ID, values );

Modify data

DB. insert (dbhelper. tb_name, dbhelper. ID, null );
Values. Clear ();
Values. Put (dbhelper. Country, "Italy ");
Values. Put (dbhelper. Code, 39 );
DB. Update (dbhelper. tb_name, values, dbhelper. ID + "= 2", null );

Execsql execution SQL language

Db.exe csql ("insert"
+ Dbhelper. tb_name + "("
+ Dbhelper. Country + ","
+ Dbhelper. Code + ") values"
+ "('Honduras, 504 )");

Query data

Cursor c = dB. Query (dbhelper. tb_name, null,
Dbhelper. Code + "DESC ");

Delete all data

DB. Delete (dbhelper. tb_name, null, null );

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.