您的位置:首页 > 移动开发 > Android开发

Android:SQLiteOpenHelper 学习笔记

2012-03-11 02:13 441 查看

SQLiteOpenHelper

一个帮助类,用于帮助用户创建数据库和数据库的版本管理。

使用的时候,子类必须实现其onCreate(),onUpgrade()两个方法(这个类是抽象类,这两个方法是抽象方法)。

在打开或操作数据库的时候,必须保证数据库存在。

getReadableDatabase

public SQLiteDatabase getReadableDatabase()

Create and/or open a database. This will be the same object returned by
getWritableDatabase()
unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to

getWritableDatabase()
may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.创建或者打开一个数据库。这个和由getWriteableDtabase()方法返回的对象是同一个;有的时候,在磁盘空间已经满的时候,就是只读状态。

getWritableDatabase

public SQLiteDatabase getWritableDatabase()

Create and/or open a database that will be used for reading and writing. Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. Make sure to call
close()
when you no longer need it.
Errors such as bad permissions or a full disk may cause this operation to fail, but future attempts may succeed if the problem is fixed.

创建或者打开一个数据库,用于读和写。

在第一次打开成功的时候,数据库将会被打开;(调用 oncreate() onUpgrade() )

在你每次写数据到数据库的时候,都会调用这个方法。

确保你以后不使用的情况下,调用close()方法。

像不健康的许可或者磁盘已满,将会导致方法运行失败,但是在之后问题解决了,将来的操作可能会成功。





onCreate

public abstract void onCreate(SQLiteDatabase db)

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.
在数据库第一次创建的时候,调用此方法。表格的创建和表格的初始化,都在这里完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: