Java Code Examples for android.database.sqlite.SQLiteDatabase#setForeignKeyConstraintsEnabled()

The following examples show how to use android.database.sqlite.SQLiteDatabase#setForeignKeyConstraintsEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidDriver.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: SchemaManager.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 3
Source File: DevicesOpenHelper.java    From device-database with Apache License 2.0 5 votes vote down vote up
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onConfigure(SQLiteDatabase db) {
    super.onConfigure(db);

    setWriteAheadLoggingEnabled(true);
    db.setForeignKeyConstraintsEnabled(true);
}
 
Example 4
Source File: Database.java    From OpenXiaomiScale with Apache License 2.0 5 votes vote down vote up
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();
	}
}
 
Example 5
Source File: Database.java    From OpenXiaomiScale with Apache License 2.0 5 votes vote down vote up
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();
	}

}
 
Example 6
Source File: SqlHelper.java    From Aria with Apache License 2.0 5 votes vote down vote up
@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;");
  }
}
 
Example 7
Source File: ServerDatabaseHandler.java    From an2linuxclient with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onConfigure(SQLiteDatabase db){
    db.setForeignKeyConstraintsEnabled(true);
}
 
Example 8
Source File: ServerDatabaseHandler.java    From an2linuxclient with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onConfigure(SQLiteDatabase db){
    db.setForeignKeyConstraintsEnabled(true);
}
 
Example 9
Source File: BaseDb.java    From tindroid with Apache License 2.0 4 votes vote down vote up
@Override
public void onConfigure(SQLiteDatabase db) {
    db.setForeignKeyConstraintsEnabled(true);
}
 
Example 10
Source File: SQLiteDatabaseCompat.java    From stetho with MIT License 4 votes vote down vote up
@Override
public void enableFeatures(@SQLiteOpenOptions int openOptions, SQLiteDatabase db) {
  if ((openOptions & ENABLE_FOREIGN_KEY_CONSTRAINTS) != 0) {
    db.setForeignKeyConstraintsEnabled(true);
  }
}
 
Example 11
Source File: DaoManager.java    From sqlbrite-dao with Apache License 2.0 4 votes vote down vote up
@Override public void onConfigure(SQLiteDatabase db) {
  super.onConfigure(db);
  if (foreignKeyConstraints) {
    db.setForeignKeyConstraintsEnabled(true);
  }
}