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 );