一、事务支持
Android对事务的支持主要有以下几个接口:
voidbeginTransaction()
以 EXCLUSIVE 模式开始一个事务.
voidbeginTransactionNonExclusive()
以 IMMEDIATE 模式开始一个事务.
voidbeginTransactionWithListener(SQLiteTransactionListener transactionListener)
以 EXCLUSIVE 模式开始一个事务.
voidbeginTransactionWithListenerNonExclusive(SQLiteTransactionListener transactionListener)
以 IMMEDIATE 模式开始一个事务.
voidsetTransactionSuccessful()
设置当前事务为成功状态.
voidendTransaction()
结束当前事务.
注:EXCLUSIVE模式与IMMEDIATE模式的区别:
EXCLUSIVE代表排他的、独立的,当我们用这种模式获取事务时,在我们的事务没有结束之前,其他的线程和进程是既不能读取该数据库,也不能对数据库进行任何写操作;IMMEDIATE代表即时的,当我们用这种模式获取事务的时候,其他进程和线程无法写入该数据库,但是却可以正常的读取。
android下事务操作的一般模式是:
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
二、约束
sqlite支持多种数据约束:
NOT NULL - 非空
UNIQUE - 唯一
PRIMARY KEY - 主键
FOREIGN KEY - 外键
CHECK - 条件检查
DEFAULT - 默认
当向数据库中插入或更新数据时,如果有约束冲突发生,可以通过设置某些标志来决定如何处理冲突,其接口如下:
longinsertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm)
intupdateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm)
可以提供的标志有:
CONFLICT_ABORT When a constraint violation occurs,no ROLLBACK is executed so changes from prior commands within the same transaction are preserved.
CONFLICT_FAIL When a constraint violation occurs, the command aborts with a return code SQLITE_CONSTRAINT.
CONFLICT_IGNORE When a constraint violation occurs, the one row that contains the constraint violation is not inserted or changed.
CONFLICT_NONE Use the following when no conflict action is specified.
CONFLICT_REPLACE When a UNIQUE constraint violation occurs, the pre-existing rows that are causing the constraint violation are removed prior to inserting or updating the current row.
CONFLICT_ROLLBACK When a constraint violation occurs, an immediate ROLLBACK occurs, thus ending the current transaction, and the command aborts with a return code of SQLITE_CONSTRAINT.
参考文章:http://www.cnblogs.com/myqiao/archive/2011/07/13/2105550.html
三、外键支持
可以设置sqlite,使其支持外键,接口如下:
voidsetForeignKeyConstraintsEnabled(boolean enable)
注:该接口最好在SqLiteOpenHelpe的onConfig()接口中调用
四、数据类型
参考文章:http://sqlite.org/datatype3.html
五、推荐一个图形化Sqlite工具:sqlite administrator