Android之SQLiteOpenHelper应用

移动开发 Android
SQliteOpenHelper是一个抽象类,来管理数据库的创建和版本的管理。要使用它必须实现它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法

1.SQLiteOpenHelper

  SQliteOpenHelper是一个抽象类,来管理数据库的创建和版本的管理。要使用它必须实现它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法

  onCreate:当数据库***次被建立的时候被执行,例如创建表,初始化数据等。

  onUpgrade:当数据库需要被更新的时候执行,例如删除久表,创建新表。

2.实现代码

  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.     //数据库版本   
  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的类,它封装了一些操作数据库的API。使用它能实现基本的CRUD操作,通过getWritableDatabase()和getReadableDatabase()可以获取数据库实例。

4.实现代码

 

  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. //     * 新建一个数据库   
  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.      * 插入数据  
  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.      * 更新数据  
  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.      * 删除数据  
  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.      * 打开数据库  
  148.      */   
  149.     public void OpenDb(){   
  150.         dbHelper = new DBHelper(this"TestDb01");   
  151.         db = dbHelper.getWritableDatabase();   
  152.     }   
  153.        
  154.     /**  
  155.      * 关闭数据库  
  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 进入命令模式

  5.2 cd 文件名 进入文件

  5.3 ls或ls -l 查看目录下的文件

  5.4 sqlite3 数据库名 进入数据库

  5.5 .schema 查看数据库下的信息

  5.6 ctrl+d 退出sqlite模式

本文链接:http://blog.csdn.net/cycwind/article/details/6960501#

责任编辑:chenqingxiang 来源: oschina
相关推荐

2015-02-27 16:35:13

智能农业Android界面

2012-04-25 21:54:09

Android

2015-02-03 14:45:55

android全局异常

2015-02-11 17:49:35

Android源码自定义控件

2015-02-28 15:15:47

插件Android桌面插件

2014-03-06 13:26:49

动画资源Animation R

2013-05-20 17:51:47

Android游戏开发SurfaceView

2013-05-20 17:13:17

Android游戏开发CanvasPaint

2013-07-25 15:05:00

Android模拟器Genymotion

2015-03-30 14:24:06

网易布局

2021-08-11 17:15:17

AndroidActivity场景

2011-02-16 14:15:58

FringAndroid应用iOS应用

2014-04-29 14:08:40

OpenGL ESAndroid应用投影

2012-04-25 22:52:40

2021-08-16 17:15:19

设计模式Android适配器模式

2010-01-28 11:26:39

Android log

2010-01-27 17:45:15

Android应用技巧

2011-03-08 16:30:24

Proftpd

2011-08-10 10:10:21

iPhoneUIPopoverCo

2013-08-05 17:25:40

windows
点赞
收藏

51CTO技术栈公众号