SQLite简介
SQLite是Android系统集成了的一个轻量的数据库,这是一个轻量级的数据库引擎,专门适用于资源有限的设备上适量的数据存储。
SQLiteDatabase简介
一个SQLiteDatabase就代表一个数据库,底层就是一个数据库文件,一旦应用获得了代表指定数据库的SQliteDatabase对象,就可以通过该对象来对数据库进行操作。
SQLiteDatabase数据库数据库对象的创建
SQLiteDatabase提供了下列静态方法打开一个文件对应的数据库。
static SQLiteDatabase openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags); 打开path路径对应的SQlite数据库。
static SQLiteDatabase openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory); 打开或者创建一个文件file对应的SQlite数据库。
static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory); 打开或者创建一个path文件所代表的SQLite数据库。
注:这里的SQLiteDatabase.CursorFactory表示允许在数据对象调用查询的时候返回一个Cursor的子类对象。指定文件名创建数据库对象时,文件的后缀名没有硬性要求,可有可无,原因是android基于linux,而在linux中文件的后缀名没有windows中那么严格,仅仅是一个文件类型的标识而已。
SqliteDatabase的数据库操作的常用方法
对于所有的数据增删查改方法,基本的原理就是对sql语句进行了封装,使其对象化和函数化,如果学过J2EE的持久化层框架,那么很容易理解这里的处理过程与Hibernate和Mybatis基本相同。具体的使用实例可以参考这篇文章(http://blog.youkuaiyun.com/liuhe688/article/details/6715983),分析的很详细。
int |
delete(
String table,
String whereClause,
String[] whereArgs)
Convenience method for deleting rows in the database.
|
static boolean |
deleteDatabase(
File file)
Deletes a database including its journal file and other auxiliary files that may have been created by the database engine.
|
void |
execSQL(
String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
|
void |
execSQL(
String sql,
Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
|
long |
getMaximumSize()
Returns the maximum size the database may grow to.
|
final String |
getPath()
Gets the path to the database file.
|
int |
getVersion()
Gets the database version.
|
long |
insert(
String table,
String nullColumnHack,
ContentValues values)
Convenience method for inserting a row into the database.
|
long |
insertOrThrow(
String table,
String nullColumnHack,
ContentValues values)
Convenience method for inserting a row into the database.
|
boolean |
isOpen()
Returns true if the database is currently open.
|
boolean |
isReadOnly()
Returns true if the database is opened as read only.
|
boolean |
needUpgrade(int newVersion)
Returns true if the new version code is greater than the current database version.
|
Cursor |
query(
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
Query the given table, returning a
Cursor over the result set.
|
Cursor |
query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit,
CancellationSignal cancellationSignal)
Query the given URL, returning a
Cursor over the result set.
|
Cursor |
query(
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
Query the given table, returning a
Cursor over the result set.
|
Cursor |
query(boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy,
String limit)
Query the given URL, returning a
Cursor over the result set.
|
Cursor |
rawQuery(
String sql,
String[] selectionArgs,
CancellationSignal cancellationSignal)
Runs the provided SQL and returns a
Cursor over the result set.
|
Cursor |
rawQuery(
String sql,
String[] selectionArgs)
Runs the provided SQL and returns a
Cursor over the result set.
|
void |
setMaxSqlCacheSize(int cacheSize)
Sets the maximum size of the prepared-statement cache for this database.
|
long |
setMaximumSize(long numBytes)
Sets the maximum size the database will grow to.
|
void |
setVersion(int version)
Sets the database version.
|
int |
update(
String table,
ContentValues values,
String whereClause,
String[] whereArgs)
Convenience method for updating rows in the database.
|
SQLite的数据库事务
SQLite事务(Transaction)和其他所有数据库的事务一样,是并发控制的基本单位。所谓的事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位。例如,银行转账工作:从一个账号扣款并使另一个账号增款,这两个操作要么都执行,要么都不执行。所以,应该把它们看成一个事务。事务是数据库维护数据一致性的单位,在每个事务结束时,都能保持数据一致性。
使用事务的常用模式如下
db.beginTransaction(); try { ... db.setTransactionSuccessful(); } finally { db.endTransaction(); }
SQliteDatabase的数据库事务操作方法包括下列一些,四种启动方式的区别,官方文档并没有明确说明。统一的说明是允许事务嵌套使用,所有的嵌套事务会同时成功或失败,如果某个嵌套事务没有调用isTransactioinSuccessful(),那么所有的事务都会回滚,否则,所有的事务都会提交。
void |
beginTransaction()
Begins a transaction in EXCLUSIVE mode.
|
void |
beginTransactionNonExclusive()
Begins a transaction in IMMEDIATE mode.
|
void |
beginTransactionWithListener(
SQLiteTransactionListener transactionListener)
Begins a transaction in EXCLUSIVE mode.
|
void |
beginTransactionWithListenerNonExclusive(
SQLiteTransactionListener transactionListener)
Begins a transaction in IMMEDIATE mode.
|
void |
endTransaction()
End a transaction.
|
boolean |
inTransaction()
Returns true if the current thread has a transaction pending.
|