Java Code Examples for android.database.sqlite.SQLiteDatabase#setVersion()
The following examples show how to use
android.database.sqlite.SQLiteDatabase#setVersion() .
These examples are extracted from open source projects.
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 Project: android_9.0.0_r45 File: DatabaseUtils.java License: Apache License 2.0 | 5 votes |
/** * Creates a db and populates it with the sql statements in sqlStatements. * * @param context the context to use to create the db * @param dbName the name of the db to create * @param dbVersion the version to set on the db * @param sqlStatements the statements to use to populate the db. This should be a single string * of the form returned by sqlite3's <tt>.dump</tt> command (statements separated by * semicolons) */ static public void createDbFromSqlStatements( Context context, String dbName, int dbVersion, String sqlStatements) { SQLiteDatabase db = context.openOrCreateDatabase(dbName, 0, null); // TODO: this is not quite safe since it assumes that all semicolons at the end of a line // terminate statements. It is possible that a text field contains ;\n. We will have to fix // this if that turns out to be a problem. String[] statements = TextUtils.split(sqlStatements, ";\n"); for (String statement : statements) { if (TextUtils.isEmpty(statement)) continue; db.execSQL(statement); } db.setVersion(dbVersion); db.close(); }
Example 2
Source Project: BigApp_Discuz_Android File: DbUtils.java License: Apache License 2.0 | 5 votes |
private synchronized static DbUtils getInstance(DaoConfig daoConfig) { DbUtils dao = daoMap.get(daoConfig.getDbName()); if (dao == null) { dao = new DbUtils(daoConfig); daoMap.put(daoConfig.getDbName(), dao); } else { dao.daoConfig = daoConfig; } // update the database if needed SQLiteDatabase database = dao.database; int oldVersion = database.getVersion(); int newVersion = daoConfig.getDbVersion(); if (oldVersion != newVersion) { if (oldVersion != 0) { DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener(); if (upgradeListener != null) { upgradeListener.onUpgrade(dao, oldVersion, newVersion); } else { try { dao.dropDb(); } catch (DbException e) { LogUtils.e(e.getMessage(), e); } } } database.setVersion(newVersion); } return dao; }
Example 3
Source Project: SqliteLookup File: AppContext.java License: Apache License 2.0 | 5 votes |
private void initDb(){ SQLiteDatabase db = openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null); int dbVersion = db.getVersion(); if(dbVersion == 0){ db.setVersion(DB_VERSION); onCreateTables(db); }else if(dbVersion < DB_VERSION){ db.setVersion(DB_VERSION); onUpdateTables(db); } }
Example 4
Source Project: android-orm-benchmark-updated File: GreenDaoExecutor.java License: Apache License 2.0 | 5 votes |
@Override public long dropDb() throws SQLException { long start = System.nanoTime(); final SQLiteDatabase sqliteDb = mHelper.getWritableDatabase(); final StandardDatabase db = new StandardDatabase(sqliteDb); DaoMaster.dropAllTables(db, true); // Reset version, so OpenHelper does not get confused sqliteDb.setVersion(0); return System.nanoTime() - start; }
Example 5
Source Project: VCL-Android File: MediaDatabase.java License: Apache License 2.0 | 5 votes |
@Override public SQLiteDatabase getWritableDatabase() { SQLiteDatabase db; try { return super.getWritableDatabase(); } catch(SQLiteException e) { try { db = SQLiteDatabase.openOrCreateDatabase(VLCApplication.getAppContext().getDatabasePath(DB_NAME), null); } catch(SQLiteException e2) { Log.w(TAG, "SQLite database could not be created! Media library cannot be saved."); db = SQLiteDatabase.create(null); } } int version = db.getVersion(); if (version != DB_VERSION) { db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { onUpgrade(db, version, DB_VERSION); } db.setVersion(DB_VERSION); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } return db; }
Example 6
Source Project: volley File: DbTools.java License: Apache License 2.0 | 5 votes |
private synchronized static DbTools getInstance(DaoConfig daoConfig) { DbTools dao = daoMap.get(daoConfig.getDbName()); if (dao == null) { dao = new DbTools(daoConfig); daoMap.put(daoConfig.getDbName(), dao); } else { dao.daoConfig = daoConfig; } // update the database if needed SQLiteDatabase database = dao.database; int oldVersion = database.getVersion(); int newVersion = daoConfig.getDbVersion(); if (oldVersion != newVersion) { if (oldVersion != 0) { DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener(); if (upgradeListener != null) { upgradeListener.onUpgrade(dao, oldVersion, newVersion); } else { try { dao.dropDb(); } catch (DbException e) { VolleyLog.e(e.getMessage(), e); } } } database.setVersion(newVersion); } return dao; }
Example 7
Source Project: android_packages_apps_GmsCore File: DatabaseHelper.java License: Apache License 2.0 | 5 votes |
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (oldVersion == DB_VERSION_OLD) { db.execSQL("DROP TABLE IF EXISTS main"); db.execSQL("DROP TABLE IF EXISTS overrides"); onCreate(db); } db.setVersion(newVersion); }
Example 8
Source Project: android-open-project-demo File: DbUtils.java License: Apache License 2.0 | 5 votes |
private synchronized static DbUtils getInstance(DaoConfig daoConfig) { DbUtils dao = daoMap.get(daoConfig.getDbName()); if (dao == null) { dao = new DbUtils(daoConfig); daoMap.put(daoConfig.getDbName(), dao); } else { dao.daoConfig = daoConfig; } // update the database if needed SQLiteDatabase database = dao.database; int oldVersion = database.getVersion(); int newVersion = daoConfig.getDbVersion(); if (oldVersion != newVersion) { //数据库升级 if (oldVersion != 0) { DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener(); if (upgradeListener != null) { upgradeListener.onUpgrade(dao, oldVersion, newVersion); } else { try { dao.dropDb(); } catch (DbException e) { LogUtils.e(e.getMessage(), e); } } } database.setVersion(newVersion); } return dao; }
Example 9
Source Project: MHViewer File: EhDB.java License: 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 List<ReadingRecord> readingRecordList = session.getReadingRecordDao().queryBuilder().list(); for (ReadingRecord record : readingRecordList) { putReadingRecord(record); } // 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 10
Source Project: android-sdcard-helper File: SQLiteSDCardHelper.java License: Apache License 2.0 | 4 votes |
private SQLiteDatabase getDatabaseLocked(boolean writable){ if (mDatabase != null) { if (!mDatabase.isOpen()) { // Darn! The user closed the database by calling mDatabase.close(). mDatabase = null; } else if (!writable || !mDatabase.isReadOnly()) { // The database is already open for business. return mDatabase; } } if (mIsInitializing) { throw new IllegalStateException("getDatabase 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. SQLiteDatabase db = mDatabase; try { mIsInitializing = true; if (db != null) { //close read-only databases, create a new writable if (writable && db.isReadOnly()) { db.close(); } } try { db = openDatabase(SQLiteDatabase.OPEN_READWRITE); } catch (SQLiteException e) { // Couldn't open the DB, let's try to create it. Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e); // Here if we fail, we propagate the exception to our user. db = openDatabase(SQLiteDatabase.OPEN_READONLY); } onConfigure(db); final int version = db.getVersion(); if (version != mNewVersion) { if (db.isReadOnly()) { throw new SQLiteException("Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + mName); } db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { if (version > mNewVersion) { onDowngrade(db, version, mNewVersion); } else { onUpgrade(db, version, mNewVersion); } } db.setVersion(mNewVersion); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } onOpen(db); if (db.isReadOnly()) { Log.w(TAG, "Opened " + mName + " in read-only mode"); } mDatabase = db; return db; }finally { mIsInitializing = false; if (db != null && db != mDatabase) { db.close(); } } }
Example 11
Source Project: NexusData File: SQLiteDatabaseHelper.java License: Apache License 2.0 | 4 votes |
/** * Create and/or open a database that will be used for reading and writing. * The first time this is called, the database will be opened and * {@link #onCreate}, {@link #onUpgrade} and/or {@link #onOpen} will be * called. * * <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> * * <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 */ 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"); } boolean success = false; SQLiteDatabase db = null; try { mIsInitializing = true; if (mPath == null) { db = SQLiteDatabase.create(null); } else { db = mContext.openOrCreateDatabase(mPath.getName(), 0, mFactory); } int version = db.getVersion(); if (version != mNewVersion) { db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { if (version > mNewVersion) { LOG.warn("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 = db; } else { if (db != null) db.close(); } } }
Example 12
Source Project: EhViewer File: EhDB.java License: 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 13
Source Project: MonsterHunter4UDatabase File: SQLiteAssetHelper.java License: MIT License | 4 votes |
/** * 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> * * <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(); } } }
Example 14
Source Project: YiBo File: SQLiteOpenHelper.java License: Apache License 2.0 | 4 votes |
/** * Create and/or open a database that will be used for reading and writing. * 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 it. * * <p>Errors such as bad permissions or a full disk may cause this operation * to fail, but future attempts may succeed if the problem is fixed.</p> * * @throws SQLiteException if the database cannot be opened for writing * @return a read/write database object valid until {@link #close} is called */ 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; try { mIsInitializing = true; if (mName == null) { db = SQLiteDatabase.create(null); } else { String path = databaseDirectory(mContext) + File.separator + mName; db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS); } int version = db.getVersion(); if (version != mNewVersion) { db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { 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 = db; } else { if (db != null) db.close(); } } }