Java Code Examples for android.database.sqlite.SQLiteDatabase#openDatabase()
The following examples show how to use
android.database.sqlite.SQLiteDatabase#openDatabase() .
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: AIMSICDDbAdapter.java From AIMSICDL with GNU General Public License v3.0 | 6 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 2
Source File: SqlHelper.java From Aria with Apache License 2.0 | 6 votes |
/** * 获取数据库连接 */ SQLiteDatabase getDb() { SQLiteDatabase db; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { SQLiteDatabase.OpenParams params = new SQLiteDatabase.OpenParams.Builder().setOpenFlags( SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY).build(); setOpenParams(params); db = getWritableDatabase(); } else { //SQLiteDatabase.openOrCreateDatabase() File dbFile = mContext.getDatabasePath(DBConfig.DB_NAME); if (!dbFile.exists()) { db = getWritableDatabase(); } else { // 触发一次SQLiteOpenHelper的流程,再使用NO_LOCALIZED_COLLATORS标志打开数据库 db = getReadableDatabase(); db.close(); db = SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY); } } db.enableWriteAheadLogging(); return db; }
Example 3
Source File: Database.java From OpenXiaomiScale with Apache License 2.0 | 6 votes |
private boolean existsDatabase() { SQLiteDatabase database = null; boolean isExist = true; try { database = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteCantOpenDatabaseException e) { // No database Log.d("Database", "Database not found!"); isExist = false; } finally { if ( database != null ) database.close(); } return isExist; }
Example 4
Source File: ContextImpl.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@Override public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) { checkMode(mode); File f = getDatabasePath(name); int flags = SQLiteDatabase.CREATE_IF_NECESSARY; if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; } if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) { flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS; } SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); setFilePermissionsFromMode(f.getPath(), mode, 0); return db; }
Example 5
Source File: ContextImpl.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
@Override public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) { checkMode(mode); File f = getDatabasePath(name); int flags = SQLiteDatabase.CREATE_IF_NECESSARY; if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; } if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) { flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS; } SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); setFilePermissionsFromMode(f.getPath(), mode, 0); return db; }
Example 6
Source File: ContextImpl.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
@Override public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) { checkMode(mode); File f = getDatabasePath(name); int flags = SQLiteDatabase.CREATE_IF_NECESSARY; if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; } if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) { flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS; } SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); setFilePermissionsFromMode(f.getPath(), mode, 0); return db; }
Example 7
Source File: TableDesignActivity.java From SqliteLookup with Apache License 2.0 | 5 votes |
@Override protected List<ColumnInfo> doInBackground(Void... params) { SQLiteDatabase db = SQLiteDatabase.openDatabase(mDbPath, null, SQLiteDatabase.OPEN_READONLY); DbSqlite dbSqlite = new DbSqlite(null, db); IBaseDao<SqliteMaster> masterDao = DaoFactory.createGenericDao(dbSqlite, SqliteMaster.class); SqliteMaster table = masterDao.queryFirstRecord("type=? and name=?", "table",mTableName); List<ColumnInfo> columnInfoList = new ArrayList<ColumnInfo>(); SqlUtils.resolveCreateSql(table.sql, columnInfoList); dbSqlite.closeDB(); return columnInfoList; }
Example 8
Source File: MBTiles.java From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License | 5 votes |
public MBTiles(String fileName) throws Exception { try { tilesDB = SQLiteDatabase.openDatabase(fileName, null, SQLiteDatabase.OPEN_READONLY); } catch(Exception ex) { Log.e("HTTPMBTiles", ex.toString()); throw (ex); } }
Example 9
Source File: SQLiteAssetHelper.java From MonsterHunter4UDatabase with MIT License | 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()}. * * @throws SQLiteException if the database cannot be opened * @return a database object valid until {@link #getWritableDatabase} * or {@link #close} is called. */ @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 10
Source File: SqliteDatabaseDriver.java From weex with Apache License 2.0 | 5 votes |
private SQLiteDatabase openDatabase(String databaseName) throws SQLiteException { Util.throwIfNull(databaseName); File databaseFile = mContext.getDatabasePath(databaseName); // Execpted to throw if it cannot open the file (for example, if it doesn't exist). return SQLiteDatabase.openDatabase(databaseFile.getAbsolutePath(), null /* cursorFactory */, SQLiteDatabase.OPEN_READWRITE); }
Example 11
Source File: DatabaseHelper.java From RetailStore with Apache License 2.0 | 5 votes |
/** * open database */ public SQLiteDatabase openDataBase() throws SQLException { // Open the database String myPath = databasePath + DB_NAME; mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); return mDataBase; }
Example 12
Source File: ContextImpl.java From AndroidComponentPlugin with Apache License 2.0 | 5 votes |
@Override public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) { File f = validateFilePath(name, true); int flags = SQLiteDatabase.CREATE_IF_NECESSARY; if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; } SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); setFilePermissionsFromMode(f.getPath(), mode, 0); return db; }
Example 13
Source File: ContextImpl.java From AndroidComponentPlugin with Apache License 2.0 | 5 votes |
@Override public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) { File f = validateFilePath(name, true); int flags = SQLiteDatabase.CREATE_IF_NECESSARY; if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; } SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); setFilePermissionsFromMode(f.getPath(), mode, 0); return db; }
Example 14
Source File: SQLiteDatabaseHelper.java From NexusData with Apache License 2.0 | 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()}. * * @throws SQLiteException if the database cannot be opened * @return a database object valid until {@link #getWritableDatabase} * or {@link #close} is called. */ 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 (mPath.getName() == null) throw e; // Can't open a temp database read-only! LOG.warn("Couldn't open " + mPath.getName() + " for writing (will try read-only):", e); } SQLiteDatabase db = null; try { mIsInitializing = true; db = SQLiteDatabase.openDatabase(mPath.toString(), mFactory, SQLiteDatabase.OPEN_READONLY); if (db.getVersion() != mNewVersion) { throw new SQLiteException("Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + mPath); } onOpen(db); LOG.warn("Opened " + mPath.getName() + " in read-only mode"); mDatabase = db; return mDatabase; } finally { mIsInitializing = false; if (db != null && db != mDatabase) db.close(); } }
Example 15
Source File: DataBaseUtils.java From UltimateAndroid with Apache License 2.0 | 5 votes |
/** * Open a database which is used for reading only. * If the database is not exists,the method will create a database from the assets{@link #copyDatabase(android.content.Context, java.io.File, String)}. * * @param context * @param databaseName * @return * @throws SQLiteException */ public synchronized SQLiteDatabase getReadableDatabase(Context context,String databaseName) throws SQLiteException{ File dbFile = context.getDatabasePath(databaseName); if (dbFile != null && !dbFile.exists()) { try { copyDatabase(context,dbFile,databaseName); } catch (IOException e) { throw new RuntimeException("Copying database error", e); } } return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY); }
Example 16
Source File: AssetDatabaseOpenHelper.java From mobile-manager-tool with MIT License | 5 votes |
/** * Create and/or open a database that will be used for reading and writing. * * @return * @throws RuntimeException if cannot copy database from assets * @throws android.database.sqlite.SQLiteException if the database cannot be opened */ public synchronized SQLiteDatabase getWritableDatabase() { File dbFile = context.getDatabasePath(databaseName); if (dbFile != null && !dbFile.exists()) { try { copyDatabase(dbFile); } catch (IOException e) { throw new RuntimeException("Error creating source database", e); } } return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READWRITE); }
Example 17
Source File: SQLiteAssetHelper.java From MonsterHunter4UDatabase with MIT License | 5 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 18
Source File: DecryptTest.java From cwac-saferoom with Apache License 2.0 | 5 votes |
private void dekey(Callable<?> decrypter) throws Exception { final Context ctxt=InstrumentationRegistry.getTargetContext(); assertEquals(SQLCipherUtils.State.DOES_NOT_EXIST, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME)); SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getWritableDatabase(); assertOriginalContent(db); db.close(); assertEquals(SQLCipherUtils.State.ENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME)); decrypter.call(); SQLiteDatabase plainDb= SQLiteDatabase.openDatabase(ctxt.getDatabasePath(DB_NAME).getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE); assertOriginalContent(plainDb); plainDb.close(); assertEquals(SQLCipherUtils.State.UNENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME)); factory = new SafeHelperFactory("".toCharArray()); helper = factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); db = helper.getReadableDatabase(); assertOriginalContent(db); db.close(); }
Example 19
Source File: EhDB.java From EhViewer with Apache License 2.0 | 4 votes |
/** * @param file The db file * @return error string, null for no error */ public static synchronized String importDB(Context context, File file) { try { SQLiteDatabase db = SQLiteDatabase.openDatabase( file.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); int newVersion = DaoMaster.SCHEMA_VERSION; int oldVersion = db.getVersion(); if (oldVersion < newVersion) { upgradeDB(db, oldVersion); db.setVersion(newVersion); } else if (oldVersion > newVersion) { return context.getString(R.string.cant_read_the_file); } DaoMaster daoMaster = new DaoMaster(db); DaoSession session = daoMaster.newSession(); // Downloads DownloadManager manager = EhApplication.getDownloadManager(context); List<DownloadInfo> downloadInfoList = session.getDownloadsDao().queryBuilder().list(); manager.addDownload(downloadInfoList); // Download label List<DownloadLabel> downloadLabelList = session.getDownloadLabelDao().queryBuilder().list(); manager.addDownloadLabel(downloadLabelList); // Download dirname List<DownloadDirname> downloadDirnameList = session.getDownloadDirnameDao().queryBuilder().list(); for (DownloadDirname dirname: downloadDirnameList) { putDownloadDirname(dirname.getGid(), dirname.getDirname()); } // History List<HistoryInfo> historyInfoList = session.getHistoryDao().queryBuilder().list(); putHistoryInfo(historyInfoList); // QuickSearch List<QuickSearch> quickSearchList = session.getQuickSearchDao().queryBuilder().list(); List<QuickSearch> currentQuickSearchList = sDaoSession.getQuickSearchDao().queryBuilder().list(); for (QuickSearch quickSearch: quickSearchList) { String name = quickSearch.name; for (QuickSearch q: currentQuickSearchList) { if (ObjectUtils.equal(q.name, name)) { // The same name name = null; break; } } if (null == name) { continue; } insertQuickSearch(quickSearch); } // LocalFavorites List<LocalFavoriteInfo> localFavoriteInfoList = session.getLocalFavoritesDao().queryBuilder().list(); for (LocalFavoriteInfo info: localFavoriteInfoList) { putLocalFavorites(info); } // Bookmarks // TODO // Filter List<Filter> filterList = session.getFilterDao().queryBuilder().list(); List<Filter> currentFilterList = sDaoSession.getFilterDao().queryBuilder().list(); for (Filter filter: filterList) { if (!currentFilterList.contains(filter)) { addFilter(filter); } } return null; } catch (Throwable e) { ExceptionUtils.throwIfFatal(e); // Ignore return context.getString(R.string.cant_read_the_file); } }
Example 20
Source File: TableViewerFragment.java From dbsync with Apache License 2.0 | 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); }