
10

SQLite数据库操作实例
source link: http://www.androidchina.net/829.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一、 SQLite介绍
SQLite是android内置的一个很小的关系型数据库。
SQLite的官网是http://www.sqlite.org/,可以去下载一些文档或相关信息。
博客中有一篇有稍微详细一点的介绍,大家可以去看一下。
二、 SQLiteOpenHelper的使用方法
SQLiteOpenHelper是一个辅助类来管理数据库的创建和版本。
可以通过继承这个类,实现它的一些方法来对数据库进行一些操作。
所有继承了这个类的类都必须实现下面这样的一个构造方法:
public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
第一个参数:Context类型,上下文对象。
第二个参数:String类型,数据库的名称
第三个参数:CursorFactory类型
第四个参数:int类型,数据库版本
下面是这个类的几个方法:
方法名 返回类型 描述 备注
getReadableDatabase()
synchronized
SQLiteDatabase 创建或打开一个数据库 可以通过这两个方法返回的SQLiteDatabase对象对数据库进行一系列的操作,如新建一个表,插入一条数据等
getWritableDatabase()
synchronized
SQLiteDatabase 创建或打开一个可以读写的数据库
onCreate(SQLiteDatabase db)
abstract
void
第一次创建的时候调用
onOpen(SQLiteDatabase db)
void
打开数据库
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion)
abstract
void
升级数据库
close()
synchronized
void
关闭所有打开的数据库对象
实现效果如下:
点击建库,就会在系统中生成一个叫DBTest的数据库,可以通过更新apk的版本来升级数据库的版本。
点击创建表,会在库中创建一个叫user的数据库表,有两个字段,name和remark。
可以通过id来更新或删除其中的数据,点击查询按钮,可以查看当前数据库中的数据。
源码下载:点击下载
转载请注明:Android开发中文站 » SQLite数据库操作实例
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK