xUtils系列之DbUtils-开启Sqlite3外键约束

实际使用中发现,Android Sqlite3数据库的外键约束不起作用,

查了一些资料发现:

SQLite在3.6.19版本中开始支持外键约束,但是为了兼容以前的程序,默认并没有启用该功能,如果要启用该功能 需要使用如下语句:
PRAGMA foreign_keys = ON 
在Android中 2.1以前的版本使用的SQLite版本是3.5.9, 在2.2版本中使用的是3.6.22.


如果需要在DbUtils中打开外键约束,需要执行如下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	ViewUtils.inject(this);
	DbUtils db=DbUtils.create(this);
	db.configAllowTransaction(true);
	db.configDebug(true);
	
	try {
		db.execNonQuery("PRAGMA foreign_keys=ON;");
	} catch (DbException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

你可能感兴趣的:(xUtils)