SQLiteOpenHelper

1、SQLite简单介绍

SQ为Structured Query (结构化查询)的缩写,Lite表示轻量级。SQLite是一款开源的关系型数据库。几乎可以支持所有现代编程语言和各种操作系统,SQLite的最新版本为SQLite 3。


SQLite的特性: 
1. ACID事务 
2. 零配置 – 无需安装和管理配置 
3. 储存在单一磁盘文件中的一个完整的数据库 
4. 数据库文件可以在不同字节顺序的机器间自由的共享 
5. 支持数据库大小至2TB 
6. 足够小, 大致3万行C代码, 250K , 运行时占用内存大概几百K。
7. 比一些流行的数据库在大部分普通数据库操作要快 
8. 简单, 轻松的API 
9. 包含TCL绑定, 同时通过Wrapper支持其他语言的绑定 
10. 良好注释的源代码, 并且有着90%以上的测试覆盖率 
11. 独立: 没有额外依赖 
12. Source完全的Open, 你可以用于任何用途, 包括出售它 
13. 支持多种开发语言,C, PHP, Perl, Java, ASP.NET,Python 



2、SQLite数据库相关操作方法

对SQLite数据库的操作一般包括:创建一个数据库,打开数据库,关闭数据库,删除数据库。


2.1、创建和打开数据库的方法:

使用openOrCreateDatabase()方法来创建,若数据库不存在,则会创建新数据库,若存在,则打开数据库。和openFileOutput(String filename,mode)的使用差不多

openOrCreateDatabase()方法的返回值为一个SQLiteDatabase对象。


2.2、关闭SQLite数据库

对数据库操作完毕之后,就要关闭数据库,否则会抛出SQLiteException异常。关闭数据库只需调用成SQLiteDatabase对象的.close()方法即可。


2.3、删除数据库

直接调用deleteDatebase()方法即可,如:

[java] view plaincopy

  1. this.deleteDatabase("mysqlite.db")  




3、SQLiteOpenHelper介绍

一般在实际开发中,为了更加方便地管理、维护、升级数据库,需要通过继承SQLiteOpenHelper类来管理SQLite数据库。

SQLiteOpenHelper可以创建数据库,和管理数据库的版本。

在继承SQLiteOpenHelper的类(extends SQLiteOpenHelper)里面,通过复写onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int) 和onOpen(SQLiteDatabase)(可选)来操作数据库。



2、SQLiteOpenHelper()的具体用法

创建一个新的class如下所示,onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法会被自动添加。

[java] view plaincopy


  1. package com.conowen.sqlite;  

  2.   

  3. import android.content.Context;  

  4. import android.database.sqlite.SQLiteDatabase;  

  5. import android.database.sqlite.SQLiteDatabase.CursorFactory;  

  6. import android.database.sqlite.SQLiteOpenHelper;  

  7.   

  8. public class DbHelper extends SQLiteOpenHelper{  

  9.   

  10.     public DbHelper(Context context, String name, CursorFactory factory,  

  11.             int version) {  

  12.         super(context, name, factory, version);  

  13.         // TODO Auto-generated constructor stub  

  14.     }  

  15.   

  16.     @Override  

  17.     public void onCreate(SQLiteDatabase db) {  

  18.         // TODO Auto-generated method stub  

  19.           

  20.     }  

  21.   

  22.     @Override  

  23.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  

  24.         // TODO Auto-generated method stub  

  25.           

  26.     }  

  27.   

  28. }  

方法详解

[java] view plaincopy

  1. public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)   

Since: API Level 1

Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one ofgetWritableDatabase() orgetReadableDatabase() is called.

Parameters
context to use to open or create the database
name of the database file, or null for an in-memory database
factory to use for creating cursor objects, or null for the default
version number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database; if the database is newer, onDowngrade(SQLiteDatabase, int, int) will be used to downgrade the database

参数简述:

name————表示数据库文件名(不包括文件路径),SQLiteOpenHelper类会根据这个文件名来创建数据库文件。

version————表示数据库的版本号。如果当前传入的数据库版本号比上一次创建的版本高,SQLiteOpenHelper就会调用onUpgrade()方法。


[java] view plaincopy

  1. public DbHelper(Context context, String name, CursorFactory factory,  

  2.             int version) {  

  3.         super(context, name, factory, version);  

  4.         // TODO Auto-generated constructor stub  

  5.     }  


以上是SQLiteOpenHelper 的构造函数,当数据库不存在时,就会创建数据库,然后打开数据库(过程已经被封装起来了),再调用onCreate (SQLiteDatabase db)方法来执行创建表之类的操作。当数据库存在时,SQLiteOpenHelper 就不会调用onCreate (SQLiteDatabase db)方法了,它会检测版本号,若传入的版本号高于当前的,就会执行onUpgrade()方法来更新数据库和版本号。


3、SQLiteOpenHelper的两个主要方法

3.1、onCreate方法

[java] view plaincopy

  1. public abstract void onCreate (SQLiteDatabase db)<span class="normal"></span>  

Since: API Level 1

Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

Parameters
db The database.

[java] view plaincopy

  1. //这样就创建一个一个table  

  2.     @Override  

  3.     public void onCreate(SQLiteDatabase db) {  

  4.         // TODO Auto-generated method stub  

  5.   

  6.          String sql = "CREATE  TABLE table_name(_id INTEGER PRIMARY KEY , filename VARCHAR, data TEXT)";  

  7.         db.execSQL(sql);  

  8.           

  9.     }  




3.2、onUpgrade方法

[java] view plaincopy

  1. public abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)  


Since: API Level 1

Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

The SQLite ALTER TABLE documentation can be found here. If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table.

Parameters
db The database.
oldVersion The old database version.
newVersion The new database version.

        更新数据库,包括删除表,添加表等各种操作。若版本是第一版,也就是刚刚建立数据库,onUpgrade()方法里面就不用写东西,因为第一版数据库何来更新之说,以后发布的版本,数据库更新的话,可以在onUpgrade()方法添加各种更新的操作。



4、注意事项

创建完SQLiteOpenHelper 类之后,在主activity里面就可以通过SQLiteOpenHelper.getWritableDatabase()或者getReadableDatabase()方法来获取在SQLiteOpenHelper 类里面创建的数据库实例。(也就是说只有调用这两种方法才真正地实例化数据库)


getWritableDatabase() 方法————以读写方式打开数据库,如果数据库所在磁盘空间满了,而使用的又是getWritableDatabase() 方法就会出错。

                                                                         因为此时数据库就只能读而不能写,


getReadableDatabase()方法————则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,但是当打开失败后会继续尝试以只读

                                                                          方式打开数据库。而不会报错




你可能感兴趣的:(android,sqlite)