SQLiteOpenHelper

一、SQLite简单介绍html

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


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 
mysql



二、SQLite数据库相关操做方法android

对SQLite数据库的操做通常包括:建立一个数据库,打开数据库,关闭数据库,删除数据库。sql


2.一、建立和打开数据库的方法:数据库

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

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


2.二、关闭SQLite数据库app

对数据库操做完毕以后,就要关闭数据库,不然会抛出SQLiteException异常。关闭数据库只需调用成SQLiteDatabase对象的.close()方法便可。编程语言


2.三、删除数据库

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

[java] view plaincopy

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




三、SQLiteOpenHelper介绍

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

SQLiteOpenHelper能够建立数据库,和管理数据库的版本。

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



二、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()方法来更新数据库和版本号。


三、SQLiteOpenHelper的两个主要方法

3.一、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.二、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()方法添加各类更新的操做。



四、注意事项

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


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

                                                                         由于此时数据库就只能读而不能写,


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

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

相关文章
相关标签/搜索