自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android之SQLiteOpenHelper應(yīng)用

移動開發(fā) Android
SQliteOpenHelper是一個抽象類,來管理數(shù)據(jù)庫的創(chuàng)建和版本的管理。要使用它必須實(shí)現(xiàn)它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法

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)代碼

  1. package xqh.utils;   
  2.    
  3. import android.content.Context;   
  4. import android.database.sqlite.SQLiteDatabase;   
  5. import android.database.sqlite.SQLiteOpenHelper;   
  6. import android.database.sqlite.SQLiteDatabase.CursorFactory;   
  7.    
  8. public class DBHelper extends SQLiteOpenHelper {   
  9.    
  10.     //數(shù)據(jù)庫版本   
  11.     private static final int VERSION = 1;   
  12.     //新建一個表   
  13.     String sql = "create table if not exists TestUsers"+   
  14.     "(id int primary key,name varchar,sex varchar)";   
  15.        
  16.     public DBHelper(Context context, String name, CursorFactory factory,   
  17.             int version) {   
  18.         super(context, name, factory, version);   
  19.     }   
  20.    
  21.     public DBHelper(Context context,String name,int version){   
  22.         this(context,name,null,version);   
  23.     }   
  24.        
  25.     public DBHelper(Context context,String name){   
  26.         this(context,name,VERSION);   
  27.     }   
  28.        
  29.     @Override   
  30.     public void onCreate(SQLiteDatabase db) {   
  31.         db.execSQL(sql);   
  32.     }   
  33.    
  34.     @Override   
  35.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   
  36.            
  37.     }   
  38.        
  39. }   

3.SQLite的使用

  Android提供了一個名為SQLiteDatabase的類,它封裝了一些操作數(shù)據(jù)庫的API。使用它能實(shí)現(xiàn)基本的CRUD操作,通過getWritableDatabase()和getReadableDatabase()可以獲取數(shù)據(jù)庫實(shí)例。

4.實(shí)現(xiàn)代碼

 

  1. package xqh.sqlite;   
  2.    
  3. import xqh.utils.DBHelper;   
  4. import android.app.Activity;   
  5. import android.database.SQLException;   
  6. import android.database.sqlite.SQLiteDatabase;   
  7. import android.os.Bundle;   
  8. import android.widget.Button;   
  9. import android.util.Log;   
  10. import android.view.View;   
  11. import android.view.View.OnClickListener;;   
  12.    
  13. public class TestSQLite extends Activity {   
  14.    
  15.     Button textBtn = null;   
  16.     Button btnCreateDb = null;   
  17.     Button btnCreateTb = null;   
  18.     Button btnInsert = null;   
  19.     Button btnUpdate = null;   
  20.     Button btnDelete = null;   
  21.     DBHelper dbHelper = null;   
  22.     SQLiteDatabase db = null;   
  23.        
  24.     @Override   
  25.     protected void onCreate(Bundle savedInstanceState) {   
  26.         // TODO Auto-generated method stub   
  27.         super.onCreate(savedInstanceState);   
  28.         setContentView(R.layout.sqlitetest);   
  29.            
  30.         OpenDb();   
  31.            
  32.         textBtn = (Button)findViewById(R.id.btnHeader);   
  33.         textBtn.setFocusable(true);   
  34.            
  35. //        btnCreateDb = (Button)findViewById(R.id.btnCreateDb);   
  36. //        btnCreateDb.setOnClickListener(createDbListener);   
  37. //           
  38. //        btnCreateTb = (Button)findViewById(R.id.btnCreateTb);   
  39. //        btnCreateTb.setOnClickListener(createTbListener);   
  40.            
  41.         btnInsert = (Button)findViewById(R.id.btnInsert);   
  42.         btnInsert.setOnClickListener(insertTbListener);   
  43.            
  44.         btnUpdate = (Button)findViewById(R.id.btnUpdate);   
  45.         btnUpdate.setOnClickListener(updateTbListener);   
  46.            
  47.         btnDelete = (Button)findViewById(R.id.btnDelete);   
  48.         btnDelete.setOnClickListener(deleteTbListener);   
  49.    
  50.     }   
  51.        
  52.     public OnClickListener deleteTbListener = new OnClickListener() {   
  53.         public void onClick(View v) {   
  54.             DeleteTb();   
  55.         }   
  56.     };   
  57.        
  58.     public OnClickListener updateTbListener = new OnClickListener() {   
  59.         public void onClick(View v) {   
  60.             UpdateTb();   
  61.         }   
  62.     };   
  63.        
  64.     public OnClickListener insertTbListener = new OnClickListener() {   
  65.         public void onClick(View v) {   
  66.             InsertTb();   
  67.         }   
  68.     };   
  69.        
  70. //    public OnClickListener createDbListener = new OnClickListener() {   
  71. //        public void onClick(View v) {   
  72. //            CreateDatabase("TestDb01");   
  73. //        }   
  74. //    };   
  75.    
  76. //    public OnClickListener createTbListener = new OnClickListener() {   
  77. //        public void onClick(View v) {   
  78. //            CreateTable();   
  79. //        }   
  80. //    };   
  81.        
  82. //    /**   
  83. //     * 新建一個數(shù)據(jù)庫   
  84. //     * @param dbName   
  85. //     * @return   
  86. //     */   
  87. //    public SQLiteDatabase CreateDatabase(String dbName){   
  88. //        dbHelper = new DBHelper(this, dbName);   
  89. //        return dbHelper.getWritableDatabase();   
  90. //    }   
  91.        
  92.     /**  
  93.      * 新建一個表  
  94.      * @param db  
  95.      */   
  96.     public void CreateTable(){   
  97.         db = dbHelper.getWritableDatabase();   
  98.         String sql = "create table if not exists TestUsers"+   
  99.                         "(id int primary key,name varchar,sex varchar)";   
  100.         try {   
  101.             db.execSQL(sql);   
  102.         } catch (SQLException e) {   
  103.             Log.i("err""create table failed");   
  104.         }   
  105.     }   
  106.        
  107.     /**  
  108.      * 插入數(shù)據(jù)  
  109.      */   
  110.     public void InsertTb(){   
  111.         db = dbHelper.getWritableDatabase();   
  112.         String sql = "insert into TestUsers (id,name,sex) values (2,'hongguang','men')";   
  113.         try {   
  114.             db.execSQL(sql);   
  115.         } catch (SQLException e) {   
  116.             Log.i("err""insert failed");   
  117.         }   
  118.     }   
  119.        
  120.     /**  
  121.      * 更新數(shù)據(jù)  
  122.      */   
  123.     public void UpdateTb() {   
  124.         db = dbHelper.getWritableDatabase();   
  125.         String sql = "Update TestUsers set name = 'anhong',sex = 'men' where id = 2";   
  126.         try {   
  127.             db.execSQL(sql);   
  128.         } catch (SQLException e) {   
  129.             Log.i("err""update failed");   
  130.         }   
  131.     }   
  132.        
  133.     /**  
  134.      * 刪除數(shù)據(jù)  
  135.      */   
  136.     public void DeleteTb(){   
  137.         db = dbHelper.getWritableDatabase();   
  138.         String sql = "delete from TestUsers where id = 2";   
  139.         try {   
  140.             db.execSQL(sql);   
  141.         } catch (SQLException e) {   
  142.             Log.i("err""delete failed");   
  143.         }   
  144.     }   
  145.        
  146.     /**  
  147.      * 打開數(shù)據(jù)庫  
  148.      */   
  149.     public void OpenDb(){   
  150.         dbHelper = new DBHelper(this"TestDb01");   
  151.         db = dbHelper.getWritableDatabase();   
  152.     }   
  153.        
  154.     /**  
  155.      * 關(guān)閉數(shù)據(jù)庫  
  156.      */   
  157.     public void CloseDb(){   
  158.         dbHelper.close();   
  159.     }   
  160.        
  161.     @Override   
  162.     protected void onDestroy() {   
  163.         super.onDestroy();   
  164.         if(db!=null){   
  165.             db.close();   
  166.         }   
  167.         if(dbHelper!=null){   
  168.             dbHelper.close();   
  169.         }   
  170.     }   
  171.        
  172. }   

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#

責(zé)任編輯:chenqingxiang 來源: oschina
相關(guān)推薦

2015-02-27 16:35:13

智能農(nóng)業(yè)Android界面

2012-04-25 21:54:09

Android

2015-02-03 14:45:55

android全局異常

2014-03-06 13:26:49

動畫資源Animation R

2015-02-11 17:49:35

Android源碼自定義控件

2015-02-28 15:15:47

插件Android桌面插件

2013-05-20 17:51:47

Android游戲開發(fā)SurfaceView

2013-07-25 15:05:00

Android模擬器Genymotion

2013-05-20 17:13:17

Android游戲開發(fā)CanvasPaint

2015-03-30 14:24:06

網(wǎng)易布局

2021-08-11 17:15:17

AndroidActivity場景

2014-04-29 14:08:40

OpenGL ESAndroid應(yīng)用投影

2011-02-16 14:15:58

FringAndroid應(yīng)用iOS應(yīng)用

2013-09-02 16:13:56

API應(yīng)用資源

2013-08-05 17:25:40

windows

2011-03-08 16:30:24

Proftpd

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-03-25 11:06:46

2021-08-16 17:15:19

設(shè)計(jì)模式Android適配器模式

2014-07-29 15:57:01

ContentProv
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號