Android之SQLiteOpenHelper應(yīng)用
1.SQLiteOpenHelper
SQliteOpenHelper是一個抽象類,來管理數(shù)據(jù)庫的創(chuàng)建和版本的管理。要使用它必須實(shí)現(xiàn)它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法
onCreate:當(dāng)數(shù)據(jù)庫***次被建立的時候被執(zhí)行,例如創(chuàng)建表,初始化數(shù)據(jù)等。
onUpgrade:當(dāng)數(shù)據(jù)庫需要被更新的時候執(zhí)行,例如刪除久表,創(chuàng)建新表。
2.實(shí)現(xiàn)代碼
- package xqh.utils;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- public class DBHelper extends SQLiteOpenHelper {
- //數(shù)據(jù)庫版本
- private static final int VERSION = 1;
- //新建一個表
- String sql = "create table if not exists TestUsers"+
- "(id int primary key,name varchar,sex varchar)";
- public DBHelper(Context context, String name, CursorFactory factory,
- int version) {
- super(context, name, factory, version);
- }
- public DBHelper(Context context,String name,int version){
- this(context,name,null,version);
- }
- public DBHelper(Context context,String name){
- this(context,name,VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(sql);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- }
3.SQLite的使用
Android提供了一個名為SQLiteDatabase的類,它封裝了一些操作數(shù)據(jù)庫的API。使用它能實(shí)現(xiàn)基本的CRUD操作,通過getWritableDatabase()和getReadableDatabase()可以獲取數(shù)據(jù)庫實(shí)例。
4.實(shí)現(xiàn)代碼
- package xqh.sqlite;
- import xqh.utils.DBHelper;
- import android.app.Activity;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.widget.Button;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;;
- public class TestSQLite extends Activity {
- Button textBtn = null;
- Button btnCreateDb = null;
- Button btnCreateTb = null;
- Button btnInsert = null;
- Button btnUpdate = null;
- Button btnDelete = null;
- DBHelper dbHelper = null;
- SQLiteDatabase db = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sqlitetest);
- OpenDb();
- textBtn = (Button)findViewById(R.id.btnHeader);
- textBtn.setFocusable(true);
- // btnCreateDb = (Button)findViewById(R.id.btnCreateDb);
- // btnCreateDb.setOnClickListener(createDbListener);
- //
- // btnCreateTb = (Button)findViewById(R.id.btnCreateTb);
- // btnCreateTb.setOnClickListener(createTbListener);
- btnInsert = (Button)findViewById(R.id.btnInsert);
- btnInsert.setOnClickListener(insertTbListener);
- btnUpdate = (Button)findViewById(R.id.btnUpdate);
- btnUpdate.setOnClickListener(updateTbListener);
- btnDelete = (Button)findViewById(R.id.btnDelete);
- btnDelete.setOnClickListener(deleteTbListener);
- }
- public OnClickListener deleteTbListener = new OnClickListener() {
- public void onClick(View v) {
- DeleteTb();
- }
- };
- public OnClickListener updateTbListener = new OnClickListener() {
- public void onClick(View v) {
- UpdateTb();
- }
- };
- public OnClickListener insertTbListener = new OnClickListener() {
- public void onClick(View v) {
- InsertTb();
- }
- };
- // public OnClickListener createDbListener = new OnClickListener() {
- // public void onClick(View v) {
- // CreateDatabase("TestDb01");
- // }
- // };
- // public OnClickListener createTbListener = new OnClickListener() {
- // public void onClick(View v) {
- // CreateTable();
- // }
- // };
- // /**
- // * 新建一個數(shù)據(jù)庫
- // * @param dbName
- // * @return
- // */
- // public SQLiteDatabase CreateDatabase(String dbName){
- // dbHelper = new DBHelper(this, dbName);
- // return dbHelper.getWritableDatabase();
- // }
- /**
- * 新建一個表
- * @param db
- */
- public void CreateTable(){
- db = dbHelper.getWritableDatabase();
- String sql = "create table if not exists TestUsers"+
- "(id int primary key,name varchar,sex varchar)";
- try {
- db.execSQL(sql);
- } catch (SQLException e) {
- Log.i("err", "create table failed");
- }
- }
- /**
- * 插入數(shù)據(jù)
- */
- public void InsertTb(){
- db = dbHelper.getWritableDatabase();
- String sql = "insert into TestUsers (id,name,sex) values (2,'hongguang','men')";
- try {
- db.execSQL(sql);
- } catch (SQLException e) {
- Log.i("err", "insert failed");
- }
- }
- /**
- * 更新數(shù)據(jù)
- */
- public void UpdateTb() {
- db = dbHelper.getWritableDatabase();
- String sql = "Update TestUsers set name = 'anhong',sex = 'men' where id = 2";
- try {
- db.execSQL(sql);
- } catch (SQLException e) {
- Log.i("err", "update failed");
- }
- }
- /**
- * 刪除數(shù)據(jù)
- */
- public void DeleteTb(){
- db = dbHelper.getWritableDatabase();
- String sql = "delete from TestUsers where id = 2";
- try {
- db.execSQL(sql);
- } catch (SQLException e) {
- Log.i("err", "delete failed");
- }
- }
- /**
- * 打開數(shù)據(jù)庫
- */
- public void OpenDb(){
- dbHelper = new DBHelper(this, "TestDb01");
- db = dbHelper.getWritableDatabase();
- }
- /**
- * 關(guān)閉數(shù)據(jù)庫
- */
- public void CloseDb(){
- dbHelper.close();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if(db!=null){
- db.close();
- }
- if(dbHelper!=null){
- dbHelper.close();
- }
- }
- }
5.一些SQLite操作命令
5.1 adb shell 進(jìn)入命令模式
5.2 cd 文件名 進(jìn)入文件
5.3 ls或ls -l 查看目錄下的文件
5.4 sqlite3 數(shù)據(jù)庫名 進(jìn)入數(shù)據(jù)庫
5.5 .schema 查看數(shù)據(jù)庫下的信息
5.6 ctrl+d 退出sqlite模式
本文鏈接:http://blog.csdn.net/cycwind/article/details/6960501#