下面列出了android.database.sqlite.SQLiteDatabase#setForeignKeyConstraintsEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public SQLiteDb open(String path, boolean readOnly, boolean fullMutex) {
int flags = 0;
if (readOnly) {
flags |= SQLiteDatabase.OPEN_READONLY;
} else {
flags |= SQLiteDatabase.OPEN_READWRITE;
flags |= SQLiteDatabase.CREATE_IF_NECESSARY;
}
final SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, flags);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
db.setForeignKeyConstraintsEnabled(true);
} else {
db.execSQL("PRAGMA foreign_keys = ON;");
}
return new AndroidDb(db);
}
@Override
public void onConfigure(SQLiteDatabase db) {
// Note that this is only called automatically by the SQLiteOpenHelper base class on Jelly
// Bean and above.
configured = true;
db.rawQuery("PRAGMA busy_timeout=0;", new String[0]).close();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
db.setForeignKeyConstraintsEnabled(true);
}
}
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
setWriteAheadLoggingEnabled(true);
db.setForeignKeyConstraintsEnabled(true);
}
public void insertElement (WeightDBElement element)
{
Log.d("Database", "Insert element called!");
SQLiteDatabase database = null;
try
{
database = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
database.setForeignKeyConstraintsEnabled(true);
database.execSQL(
"INSERT INTO " + WEIGHT_TABLE_NAME + " VALUES (" +
element.unixTime + "," +
element.weight + "," +
element.weightUnit + "," +
element.userID + ")"
);
}
catch (SQLiteException e)
{
// Unhandled exception!
Log.d("Database", "Exception in inserting weight element in database");
e.printStackTrace();
System.exit(-1);
}
finally
{
if ( database != null )
database.close();
}
}
private void createDatabaseAndInitialize()
{
Log.d("Database", "Database initializing...");
SQLiteDatabase database = null;
try
{
database = activity.openOrCreateDatabase(DB_PATH, Context.MODE_PRIVATE, null);
database.setForeignKeyConstraintsEnabled(true);
// Drop previous tables
database.execSQL("DROP TABLE IF EXISTS " + WEIGHT_TABLE_NAME);
database.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
// Create new tables
database.execSQL(CREATE_USER_TABLE_QUERY);
database.execSQL(CREATE_WEIGHT_TABLE_QUERY);
// Add default user to users table, it will have id 1
database.execSQL("INSERT INTO " + USER_TABLE_NAME + " VALUES (1,'Default')");
}
catch (SQLiteException e)
{
// Unhandled exception!
Log.d("Database", "Exception in createDatabaseAndInitialize!");
e.printStackTrace();
System.exit(-1);
}
finally
{
Log.d("Database", "Database initialize finished!");
if ( database != null )
database.close();
}
}
@Override public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
db.setForeignKeyConstraintsEnabled(true);
} else {
// SQLite在3.6.19版本中开始支持外键约束,
// 而在Android中 2.1以前的版本使用的SQLite版本是3.5.9, 在2.2版本中使用的是3.6.22.
// 但是为了兼容以前的程序,默认并没有启用该功能,如果要启用该功能
// 需要使用如下语句:
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
@Override
public void onConfigure(SQLiteDatabase db){
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void onConfigure(SQLiteDatabase db){
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void onConfigure(SQLiteDatabase db) {
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void enableFeatures(@SQLiteOpenOptions int openOptions, SQLiteDatabase db) {
if ((openOptions & ENABLE_FOREIGN_KEY_CONSTRAINTS) != 0) {
db.setForeignKeyConstraintsEnabled(true);
}
}
@Override public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
if (foreignKeyConstraints) {
db.setForeignKeyConstraintsEnabled(true);
}
}