SQLite適合移動設備的數據存儲,有處理速度快,占用資源少等優點,不需要安裝部署,內嵌到程序中作為其一部分.http://www.sqlite.org/
SQLite的數據庫文件位於/data/data/your-app-name/databases目錄下.
使用數據庫最基本就是增刪改查操作.下面的示例是使用SQLite進行增刪改查的操作.
我們需要一個輔助類繼承SQLiteOpenHelper類
- package com.sumq;
-
- import Android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteOpenHelper;
-
- public class MySQLiteHelper extends SQLiteOpenHelper{
-
- public MySQLiteHelper(Context context, String name, CursorFactory factory,
- int version) {
- super(context, name, factory, version);
- // TODO Auto-generated constructor stub
- }
-
- /*
- *當數據庫被創建時,首次執行該方法
- *一般將創建表等初始化操作放在該方法中執行
- */
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- db.execSQL("create table if not exists user_info(id integer primary key,name varchar,vip integer)");
- }
-
- /*當版本升級時調用該函數
- *參數一為要更新的數據庫
- *參數二傳入老的版本號
- *參數三傳入新的版本號
- */
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- *看需要,該方法在每次打開數據庫時被調用
- */
- @Override
- public void onOpen(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- super.onOpen(db);
- }
- }
- package com.sumq;
-
- import android.app.Activity;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.widget.TextView;
-
- public class UISQLiteActivity extends Activity {
-
- TextView tv;
- MySQLiteHelper mySQLiteHelper;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv = (TextView)findViewById(R.id.tv);
- mySQLiteHelper = new MySQLiteHelper(this, "test.db", null, 1);
- insertAndUpdateData(mySQLiteHelper);
- String result = queryData(mySQLiteHelper);
- tv.setText("名字\t等級\t"+result);
- }
-
- private void insertAndUpdateData(MySQLiteHelper mySQLiteHelper){
- //獲取數據庫對象
- SQLiteDatabase db = mySQLiteHelper.getWritableDatabase();
- //第一種插入數據庫的方法直接寫sql語句
- db.execSQL("insert into user_info(name,vip) values('user1',1)");
- //第二種調用insert方法
- ContentValues values = new ContentValues();
- values.put("name", "user2");
- values.put("vip", "2");
- db.insert("user_info", null, values);
- //更新level=2 的數據
- values.clear();
- values.put("name", "user2");
- values.put("vip", "3");
- db.update("user_info", values, "vip = ?", new String[]{"2"});
-
- db.close();
- }
-
- private String queryData(MySQLiteHelper mySQLiteHelper){
- String result ="";
- SQLiteDatabase db = mySQLiteHelper.getReadableDatabase();
- Cursor cursor = db.query("user_info", null, null, null, null, null, "id asc");
- while (cursor.moveToNext()) {
- result = result+cursor.getString(cursor.getColumnIndex("name"))+" ";
- result = result+cursor.getInt(cursor.getColumnIndex("vip"))+" \n";
- }
- cursor.close();
- db.close();
- return result;
- }
-
- @Override
- protected void onDestroy() {
- SQLiteDatabase db = mySQLiteHelper.getWritableDatabase();
- db.delete("user_info", "1", null);
- super.onDestroy();
- }
- }
執行程序結果顯示