Java Code Examples for android.database.sqlite.SQLiteDatabase.openDatabase()

The following are Jave code examples for showing how to use openDatabase() of the android.database.sqlite.SQLiteDatabase class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: PeSanKita-android   File: ApnDatabase.java   View Source Code Vote up 8 votes
private ApnDatabase(final Context context) throws IOException {
  this.context = context;

  File dbFile = context.getDatabasePath(DATABASE_NAME);

  if (!dbFile.getParentFile().exists() && !dbFile.getParentFile().mkdir()) {
    throw new IOException("couldn't make databases directory");
  }

  Util.copy(context.getAssets().open(ASSET_PATH, AssetManager.ACCESS_STREAMING),
            new FileOutputStream(dbFile));

  try {
    this.db = SQLiteDatabase.openDatabase(context.getDatabasePath(DATABASE_NAME).getPath(),
                                          null,
                                          SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  } catch (SQLiteException e) {
    throw new IOException(e);
  }
}
 
Example 2
Project: Cable-Android   File: ApnDatabase.java   View Source Code Vote up 8 votes
private ApnDatabase(final Context context) throws IOException {
  this.context = context;

  File dbFile = context.getDatabasePath(DATABASE_NAME);

  if (!dbFile.getParentFile().exists() && !dbFile.getParentFile().mkdir()) {
    throw new IOException("couldn't make databases directory");
  }

  Util.copy(context.getAssets().open(ASSET_PATH, AssetManager.ACCESS_STREAMING),
            new FileOutputStream(dbFile));

  try {
    this.db = SQLiteDatabase.openDatabase(context.getDatabasePath(DATABASE_NAME).getPath(),
                                          null,
                                          SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  } catch (SQLiteException e) {
    throw new IOException(e);
  }
}
 
Example 3
Project: QuranAndroid   File: DatabaseAccess.java   View Source Code Vote up 7 votes
/**
 * Function to open connection with database
 *
 * @param path database path in mobile
 * @return database object to start queries
 */
public SQLiteDatabase openDB(String path) {

    Log.d("DATABASE", path);

    SQLiteDatabase db = null;
    try {
        db = SQLiteDatabase.openDatabase(path, null, 0);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return db;

}
 
Example 4
Project: Quran   File: DatabaseHandler.java   View Source Code Vote up 7 votes
private DatabaseHandler(Context context, String databaseName) throws SQLException {
  String base = QuranFileUtils.getQuranDatabaseDirectory(context);
  if (base == null) return;
  String path = base + File.separator + databaseName;
  Crashlytics.log("opening database file: " + path);
  try {
    database = SQLiteDatabase.openDatabase(path, null,
      SQLiteDatabase.NO_LOCALIZED_COLLATORS, new DefaultDatabaseErrorHandler());
  } catch (SQLiteDatabaseCorruptException sce) {
    Crashlytics.log("corrupt database: " + databaseName);
    throw sce;
  } catch (SQLException se){
    Crashlytics.log("database file " + path +
        (new File(path).exists()? " exists" : " doesn't exist"));
    throw se;
  }

  schemaVersion = getSchemaVersion();
  matchString = "<font color=\"" +
      ContextCompat.getColor(context, R.color.translation_highlight) +
      "\">";
}
 
Example 5
Project: AIMSICDL   File: AIMSICDDbAdapter.java   View Source Code Vote up 7 votes
/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 *
 * ToDo: NOTE:  This is a dumb check, as it currently only tries to open it.
 *              It may be other reasons why it can't be opened even if it already exists.
 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;
    try {
        Log.i(TAG, mTAG + "Checking if DB exists...");
        checkDB = SQLiteDatabase.openDatabase(mDatabasePath, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        Log.i(TAG, mTAG + "SQL Exception! Database can\'t be opened: " + e);
        //Log.i(TAG, mTAG + "Database not yet created: " + e);
    }

    if (checkDB != null) {
        checkDB.close();
        Log.i(TAG, mTAG + "OK (found)");
        return true;
    }
    Log.i(TAG, mTAG + "Database probably not yet created.");
    return false;
}
 
Example 6
Project: justintrain-client-android   File: SQLiteAssetHelper.java   View Source Code Vote up 6 votes
/**
 * Create and/or open a database.  This will be the same object returned by
 * {@link #getWritableDatabase} unless some problem, such as a full disk,
 * requires the database to be opened read-only.  In that case, a read-only
 * database object will be returned.  If the problem is fixed, a future call
 * to {@link #getWritableDatabase} may succeed, in which case the read-only
 * database object will be closed and the read/write object will be returned
 * in the future.
 *
 * <p class="caution">Like {@link #getWritableDatabase}, this method may
 * take a long time to return, so you should not call it from the
 * application main thread, including from
 * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @return a database object valid until {@link #getWritableDatabase}
 * or {@link #close} is called.
 * @throws SQLiteException if the database cannot be opened
 */
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
    if (mDatabase != null && mDatabase.isOpen()) {
        return mDatabase;  // The database is already open for business
    }

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

    try {
        return getWritableDatabase();
    } catch (SQLiteException e) {
        if (mName == null) throw e;  // Can't open a temp database read-only!
        Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
    }

    SQLiteDatabase db = null;
    try {
        mIsInitializing = true;
        String path = mContext.getDatabasePath(mName).getPath();
        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
        if (db.getVersion() != mNewVersion) {
            throw new SQLiteException("Can't upgrade read-only database from version " +
                    db.getVersion() + " to " + mNewVersion + ": " + path);
        }

        onOpen(db);
        Log.w(TAG, "Opened " + mName + " in read-only mode");
        mDatabase = db;
        return mDatabase;
    } finally {
        mIsInitializing = false;
        if (db != null && db != mDatabase) db.close();
    }
}
 
Example 7
Project: justintrain-client-android   File: SQLiteAssetHelper.java   View Source Code Vote up 6 votes
private SQLiteDatabase returnDatabase() {
    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(mDatabasePath + "/" + mName, mFactory, SQLiteDatabase.OPEN_READWRITE);
        Log.i(TAG, "successfully opened database " + mName);
        return db;
    } catch (SQLiteException e) {
        Log.w(TAG, "could not open database " + mName + " - " + e.getMessage());
        return null;
    }
}
 
Example 8
Project: MyFlightbookAndroid   File: DataBaseHelper.java   View Source Code Vote up 6 votes
@SuppressWarnings("unused")
   public void openDataBase() throws SQLException {

	// Open the database
	myDataBase = SQLiteDatabase.openDatabase(mDBFileName, null,
			SQLiteDatabase.OPEN_READONLY);

}
 
Example 9
Project: Quran   File: AyahInfoDatabaseHandler.java   View Source Code Vote up 6 votes
private AyahInfoDatabaseHandler(Context context, String databaseName) throws SQLException {
  String base = QuranFileUtils.getQuranAyahDatabaseDirectory(context);
  if (base == null) {
    database = null;
  } else {
    String path = base + File.separator + databaseName;
    database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  }
}
 
Example 10
Project: Quran   File: SuraTimingDatabaseHandler.java   View Source Code Vote up 6 votes
private SuraTimingDatabaseHandler(String path) throws SQLException {
  Crashlytics.log("opening gapless data file, " + path);
  try {
    mDatabase = SQLiteDatabase.openDatabase(path, null,
        SQLiteDatabase.NO_LOCALIZED_COLLATORS, new DefaultDatabaseErrorHandler());
  } catch (SQLiteDatabaseCorruptException sce) {
    Crashlytics.log("database corrupted: " + path);
    mDatabase = null;
  } catch (SQLException se) {
    Crashlytics.log("database at " + path +
        (new File(path).exists() ? " exists" : " doesn't exist"));
    Crashlytics.logException(se);
    mDatabase = null;
  }
}
 
Example 11
Project: AIMSICDL   File: AIMSICDDbAdapter.java   View Source Code Vote up 6 votes
public AIMSICDDbAdapter(Context context) {
    super(context, DB_NAME, null, 1);
    mContext = context;
    mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
    mDatabasePath = mContext.getDatabasePath(DB_NAME).getAbsolutePath();
    mExternalFilesDirPath = mContext.getExternalFilesDir(null) + File.separator;
    //e.g. /storage/emulated/0/Android/data/zz.aimsicd.lite/

    // Create a new blank DB then write pre-compiled DB in assets folder to blank DB.
    // This will throw error on first create because there is no DB to open and this is normal.
    createDataBase();

    //return writable database
    mDb = SQLiteDatabase.openDatabase(mDatabasePath, null, SQLiteDatabase.OPEN_READWRITE);

    // This will return the database as open so we don't need to use .open . Then when app
    // is exiting we use new AIMSICDDbAdapter(getApplicationContext()).close(); to close it
    this.getWritableDatabase();
    mTables = new String[]{

            // I am trying to keep in same order and aimsicd.sql script
            // Only backing up useful tables, uncomment if you want to backup
            DBTableColumnIds.DEFAULT_LOCATION_TABLE_NAME,   // defaultLocation:     Default MCC for each country
            //DBTableColumnIds.API_KEYS_TABLE_NAME,         // API_keys:            API keys for OpenCellID, MLS etc.
            //DBTableColumnIds.COUNTER_MEASURES_TABLE_NAME, // CounterMeasures:     Counter Measures thresholds and description
            //DBTableColumnIds.DBE_CAPABILITIES_TABLE_NAME, // DBe_capabilities:    External: MNO & BTS network capabilities
            DBTableColumnIds.DBE_IMPORT_TABLE_NAME,         // DBe_import:          External: BTS import table
            DBTableColumnIds.DBI_BTS_TABLE_NAME,            // DBi_bts:             Internal: (physical) BTS data
            DBTableColumnIds.DBI_MEASURE_TABLE_NAME,        // DBi_measure:         Internal: (volatile) network measurements
            //DBTableColumnIds.DETECTION_FLAGS_TABLE_NAME,  // DetectionFlags:      Detection Flag description, settings and scoring table
            DBTableColumnIds.EVENTLOG_TABLE_NAME,           //                      Detection and general EventLog (persistent)
            //DBTableColumnIds.SECTOR_TYPE_TABLE_NAME,      // SectorType:          BTS tower sector configuration (Many CID, same BTS)
            DBTableColumnIds.DETECTION_STRINGS_TABLE_NAME,  //                      Detection strings to will be picked up in logcat
            DBTableColumnIds.SMS_DATA_TABLE_NAME,           //                      Silent SMS details
    };
}
 
Example 12
Project: editor-sql   File: SQLHelper.java   View Source Code Vote up 5 votes
public SQLHelper(String _dbFilePath, DatabaseErrorHandler errHandler) {
    this.mCurDBFilePath = _dbFilePath;
    try {
        _db = SQLiteDatabase.openDatabase(mCurDBFilePath, null, 0, errHandler);
        isDataBase = true;
    } catch (Exception e) {
        isDataBase = false;
    }
}
 
Example 13
Project: NISTGammaSearch   File: DataBaseHelper.java   View Source Code Vote up 5 votes
public boolean openDataBase(String strDBName) throws SQLException
{
    String mPath = DB_PATH + strDBName;
    //Log.v("mPath", mPath);
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}
 
Example 14
Project: sqliteAsset   File: SQLiteAssetHelper.java   View Source Code Vote up 5 votes
/**
 * Create and/or open a database.  This will be the same object returned by
 * {@link #getWritableDatabase} unless some problem, such as a full disk,
 * requires the database to be opened read-only.  In that case, a read-only
 * database object will be returned.  If the problem is fixed, a future call
 * to {@link #getWritableDatabase} may succeed, in which case the read-only
 * database object will be closed and the read/write object will be returned
 * in the future.
 *
 * <p class="caution">Like {@link #getWritableDatabase}, this method may
 * take a long time to return, so you should not call it from the
 * application main thread, including from
 * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @return a database object valid until {@link #getWritableDatabase}
 * or {@link #close} is called.
 * @throws SQLiteException if the database cannot be opened
 */
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
    if (mDatabase != null && mDatabase.isOpen()) {
        return mDatabase;  // The database is already open for business
    }

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

    try {
        return getWritableDatabase();
    } catch (SQLiteException e) {
        if (mName == null) throw e;  // Can't open a temp database read-only!
        Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
    }

    SQLiteDatabase db = null;
    try {
        mIsInitializing = true;
        String path = mContext.getDatabasePath(mName).getPath();
        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
        if (db.getVersion() != mNewVersion) {
            throw new SQLiteException("Can't upgrade read-only database from version " +
                    db.getVersion() + " to " + mNewVersion + ": " + path);
        }

        onOpen(db);
        Log.w(TAG, "Opened " + mName + " in read-only mode");
        mDatabase = db;
        return mDatabase;
    } finally {
        mIsInitializing = false;
        if (db != null && db != mDatabase) db.close();
    }
}
 
Example 15
Project: NISTGammaSearch   File: DataBaseHelper.java   View Source Code Vote up 5 votes
public boolean openDataBase() throws SQLException
{
    String mPath = DB_PATH + DB_NAME_ELEMENTS;
    //Log.v("mPath", mPath);
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}
 
Example 16
Project: Android-Bubble-Game   File: DataBaseHelper.java   View Source Code Vote up 4 votes
public void openDataBase() throws SQLException {
  // Open the database
  String myPath = DB_PATH + DB_NAME;
  myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
 
Example 17
Project: Android-Copy-Database   File: DBHelper.java   View Source Code Vote up 4 votes
boolean openDataBase() throws SQLException {
    mDataBase = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.CREATE_IF_NECESSARY);
    return mDataBase != null;
}
 
Example 18
Project: uchimznaem-helper   File: DatabaseHelper.java   View Source Code Vote up 4 votes
public boolean openDataBase() throws SQLException {
    mDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
    return mDataBase != null;
}
 
Example 19
Project: dbsync   File: TableViewerFragment.java   View Source Code Vote up 3 votes
private void openDatabase(){
    log.info("open database {}", mDbName);

    File dbFile = new File("/data/data/com.claudiodegio.dbsync.sample/databases", mDbName);

    mDB = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
}
 
Example 20
Project: MuslimMateAndroid   File: Database.java   View Source Code Vote up 2 votes
/**
 * Function to open connection with Database
 *
 * @return Database object
 */
public SQLiteDatabase openDB() {
    SQLiteDatabase db = SQLiteDatabase.openDatabase(MAIN_DATABSE, null, 0);
    return db;
}