@Override
 public final void onOpen(SQLiteDatabase db) {
   if (!db.isReadOnly()) {
     db.execSQL("PRAGMA foreign_keys = ON;");
   }
   onOpenExtra(db);
 }
 @Override
 public void onOpen(SQLiteDatabase db) {
   super.onOpen(db);
   if (!db.isReadOnly()) {
     db.execSQL("PRAGMA foreign_keys=ON;");
   }
 }
  /**
   * @param context the {@link Context} to use.
   * @return the db.
   * @throws IOException if something goes wrong.
   */
  public SQLiteDatabase getDatabase(Context context) throws IOException {
    File databaseFile;
    try {
      databaseFile = ResourcesManager.getInstance(context).getDatabaseFile();
    } catch (Exception e) {
      throw new IOException(e.getLocalizedMessage());
    }
    if (databaseHelper == null || !databaseFile.exists()) {

      databaseHelper = new DatabaseOpenHelper(databaseFile);

      SQLiteDatabase db = databaseHelper.getWritableDatabase(context);
      if (GPLog.LOG_ANDROID) {
        Log.i(DEBUG_TAG, "Database: " + db.getPath());
        Log.i(DEBUG_TAG, "Database Version: " + db.getVersion());
        Log.i(DEBUG_TAG, "Database Page Size: " + db.getPageSize());
        Log.i(DEBUG_TAG, "Database Max Size: " + db.getMaximumSize());
        Log.i(DEBUG_TAG, "Database Open?  " + db.isOpen());
        Log.i(DEBUG_TAG, "Database readonly?  " + db.isReadOnly());
        Log.i(DEBUG_TAG, "Database Locked by current thread?  " + db.isDbLockedByCurrentThread());
      }
    }

    return databaseHelper.getWritableDatabase(context);
  }
Example #4
0
  public boolean isTableExtant(String tableName) {

    mDatabase =
        SQLiteDatabase.openDatabase(
            DATABASE_PATH + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
    boolean openDb = mDatabase.isOpen();

    if (openDb) {
      if (mDatabase == null || !mDatabase.isOpen()) {
        mDatabase = getReadableDatabase();
      }

      if (!mDatabase.isReadOnly()) {
        mDatabase.close();
        mDatabase = getReadableDatabase();
      }
    }

    Cursor cursor =
        mDatabase.rawQuery(
            "select DISTINCT tbl_name from sqlite_master where tbl_name = '" + tableName + "'",
            null);
    if (cursor != null) {
      if (cursor.getCount() > 0) { // table does exist
        cursor.close();
        mDatabase.close();
        return true;
      }
      cursor.close();
    }
    mDatabase.close();
    return false;
  }
  @Override
  public void onConfigure(SQLiteDatabase db) {

    // Executes a pragma sentence to active foreign keys.
    if (!db.isReadOnly()) {
      db.execSQL("PRAGMA foreing_keys = ON;");
    }
  }
Example #6
0
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
      if (!sqLiteDatabase.isReadOnly()) {
        sqLiteDatabase.execSQL("PRAGMA foreign_key=ON");
      }

      createDatabaseTables(sqLiteDatabase);
    }
 @Override
 public void onOpen(SQLiteDatabase db) {
   super.onOpen(db);
   if (!db.isReadOnly()) {
     setForeignKeyConstraintsEnabled(db);
   }
   mOpenHelperCallbacks.onOpen(mContext, db);
 }
Example #8
0
 @Override
 public void onOpen(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   super.onOpen(db);
   if (!db.isReadOnly()) {
     // Enable foreign key constraints
     db.execSQL("PRAGMA foreign_keys=ON;");
   }
 }
  private synchronized SQLiteDatabase getWritableDatabase() {
    // create the database if it does not exist
    if (!new File(DATABASE_FILE).exists()) createDatabase();

    if (db != null && db.isOpen() && !db.isReadOnly()) return db;

    db = SQLiteDatabase.openDatabase(DATABASE_FILE, null, SQLiteDatabase.OPEN_READWRITE);

    return db;
  }
 @Override
 public void onOpen(SQLiteDatabase db) {
   super.onOpen(db);
   if (!db.isReadOnly()) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
       db.setForeignKeyConstraintsEnabled(true);
     } else {
       db.execSQL("PRAGMA foreign_keys=ON");
     }
   }
 }
Example #11
0
 public SQLiteDatabase getDatabase(boolean forceWritable) {
   try {
     SQLiteDatabase db = getReadableDatabase();
     if (forceWritable && db.isReadOnly()) {
       throw new SQLiteReadOnlyDatabaseException("Required writable database, obtained read-only");
     }
     return db;
   } catch (IllegalStateException e) {
     return this.db;
   }
 }
Example #12
0
 @Override
 public boolean onCreate() {
   dbHelper = new OrderSQLiteHelper(getContext());
   database = dbHelper.getWritableDatabase();
   if (database == null) {
     return false;
   }
   if (database.isReadOnly()) {
     database.close();
     database = null;
     return false;
   }
   return true;
 }
  @Override
  public boolean onCreate() {
    boolean ret = true;
    taskDBHelper = new TaskDBHelper(getContext());
    db = taskDBHelper.getWritableDatabase();

    if (db == null) {
      ret = false;
    }

    if (db.isReadOnly()) {
      db.close();
      db = null;
      ret = false;
    }

    return ret;
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
    if (!db.isReadOnly()) {
      // Enable foreign key constraints
      db.execSQL("PRAGMA foreign_keys=ON;");
    }

    // create all declarated tables
    db.execSQL(createUserTable);
    db.execSQL(createCocktailTable);
    db.execSQL(createCategoryTable);
    db.execSQL(createICategoryTable);
    db.execSQL(createImageTable);
    db.execSQL(createDifficultyTable);
    db.execSQL(createRatedTable);
    db.execSQL(createValidationTable);
    db.execSQL(createIngredientTable);
    db.execSQL(createConsistsOfTable);
  }
  @Override
  public boolean onCreate() {
    boolean result = true;
    mDatabaseHelper = new DatabaseHelper(getContext());
    SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();

    if (db == null) {
      result = false;
    }

    if (db != null) {
      if (db.isReadOnly()) {
        db.close();
        db = null;
        result = false;
      }
    }

    return result;
  }
  /**
   * Create and/or open a database that will be used for reading and writing. The first time this is
   * called, the database will be extracted and copied from the application's assets folder.
   *
   * <p>Once opened successfully, the database is cached, so you can call this method every time you
   * need to write to the database. (Make sure to call {@link #close} when you no longer need the
   * database.) Errors such as bad permissions or a full disk may cause this method to fail, but
   * future attempts may succeed if the problem is fixed.
   *
   * <p class="caution">Database upgrade may take a long time, you should not call this method from
   * the application main thread, including from {@link android.content.ContentProvider#onCreate
   * ContentProvider.onCreate()}.
   *
   * @throws SQLiteException if the database cannot be opened for writing
   * @return a read/write database object valid until {@link #close} is called
   */
  @Override
  public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
      return mDatabase; // The database is already open for business
    }

    if (mIsInitializing) {
      throw new IllegalStateException("getWritableDatabase called recursively");
    }

    // If we have a read-only database open, someone could be using it
    // (though they shouldn't), which would cause a lock to be held on
    // the file, and our attempts to open the database read-write would
    // fail waiting for the file lock.  To prevent that, we acquire the
    // lock on the read-only database, which shuts out other users.

    boolean success = false;
    SQLiteDatabase db = null;
    // if (mDatabase != null) mDatabase.lock();
    try {
      mIsInitializing = true;
      // if (mName == null) {
      //    db = SQLiteDatabase.create(null);
      // } else {
      //    db = mContext.openOrCreateDatabase(mName, 0, mFactory);
      // }
      db = createOrOpenDatabase(false);

      int version = db.getVersion();

      // do force upgrade
      if (version != 0 && version < mForcedUpgradeVersion) {
        db = createOrOpenDatabase(true);
        db.setVersion(mNewVersion);
        version = db.getVersion();
      }

      if (version != mNewVersion) {
        db.beginTransaction();
        try {
          if (version == 0) {
            onCreate(db);
          } else {
            if (version > mNewVersion) {
              Log.w(
                  TAG,
                  "Can't downgrade read-only database from version "
                      + version
                      + " to "
                      + mNewVersion
                      + ": "
                      + db.getPath());
            }
            onUpgrade(db, version, mNewVersion);
          }
          db.setVersion(mNewVersion);
          db.setTransactionSuccessful();
        } finally {
          db.endTransaction();
        }
      }

      onOpen(db);
      success = true;
      return db;
    } finally {
      mIsInitializing = false;
      if (success) {
        if (mDatabase != null) {
          try {
            mDatabase.close();
          } catch (Exception e) {
          }
          // mDatabase.unlock();
        }
        mDatabase = db;
      } else {
        // if (mDatabase != null) mDatabase.unlock();
        if (db != null) db.close();
      }
    }
  }
 public DataManager(SQLiteDatabase db) {
   if (!db.isOpen() || db.isReadOnly()) {
     throw new IllegalArgumentException("INVALID DATABASE: Database is closed or read-only.");
   }
   this.db = db;
 }