下面列出了android.database.sqlite.SQLiteDatabase#isReadOnly ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Run RuleMatcher.unmatch locally.
*
* @param db {@link SQLiteDatabase}
*/
private static void unmatch(final SQLiteDatabase db) {
Log.d(TAG, "unmatch()");
if (db.isReadOnly()) {
Log.e(TAG, "Database is readonly, can not unmatch on upgrade!");
return;
}
ContentValues cv = new ContentValues();
cv.put(DataProvider.Logs.PLAN_ID, DataProvider.NO_ID);
cv.put(DataProvider.Logs.RULE_ID, DataProvider.NO_ID);
// reset all but manually set plans
db.update(DataProvider.Logs.TABLE, cv, DataProvider.Logs.RULE_ID + " is null or NOT ("
+ DataProvider.Logs.RULE_ID + " = " + DataProvider.NOT_FOUND + " AND "
+ DataProvider.Logs.PLAN_ID + " != " + DataProvider.NOT_FOUND + ")", null);
cv.clear();
cv.put(DataProvider.Plans.NEXT_ALERT, 0);
db.update(DataProvider.Plans.TABLE, cv, null, null);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
if (!sqLiteDatabase.isReadOnly()){
sqLiteDatabase.execSQL("PRAGMA foreign_key=ON");
}
// sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS " +
// KEYVAL_TABLE_NAME + "(" + KEYVAL_COLUMN_KEY + " TEXT PRIMARY KEY, " +
// KEYVAL_COLUMN_VAL + " TEXT)");
sqLiteDatabase.execSQL(CREATE_TABLE_KEYVAL);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
if (!db.isReadOnly()) {
db.enableWriteAheadLogging();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
if (db.isReadOnly()) {
db = getWritableDatabase();
}
db.execSQL(DbConstants.Movies.CREATE_SQL);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
if (!db.isReadOnly()) {
db.enableWriteAheadLogging();
}
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL(ACTIVATE_FOREIGN_KEY);
}
}
@Override
public void onOpen(final SQLiteDatabase db)
{
super.onOpen(db);
if (Constants.IS_LOGGABLE)
{
Log.v(Constants.LOG_TAG, String.format("SQLite library version is: %s", DatabaseUtils.stringForQuery(db, "select sqlite_version()", null))); //$NON-NLS-1$//$NON-NLS-2$
}
if (!db.isReadOnly())
{
/*
* Enable foreign key support
*/
db.execSQL("PRAGMA foreign_keys = ON;"); //$NON-NLS-1$
// if (Constants.IS_LOGGABLE)
// {
// try
// {
// final String result1 = DatabaseUtils.stringForQuery(db, "PRAGMA foreign_keys;", null); //$NON-NLS-1$
// Log.v(Constants.LOG_TAG, String.format("Foreign keys support result was: %s", result1)); //$NON-NLS-1$
// }
// catch (final SQLiteDoneException e)
// {
// Log.w(Constants.LOG_TAG, e);
// }
// }
}
}
@Override
public void onOpen(final SQLiteDatabase db)
{
super.onOpen(db);
if (Constants.IS_LOGGABLE)
{
Log.v(Constants.LOG_TAG, String.format("SQLite library version is: %s", DatabaseUtils.stringForQuery(db, "select sqlite_version()", null))); //$NON-NLS-1$//$NON-NLS-2$
}
if (!db.isReadOnly())
{
/*
* Enable foreign key support
*/
db.execSQL("PRAGMA foreign_keys = ON;"); //$NON-NLS-1$
// if (Constants.IS_LOGGABLE)
// {
// try
// {
// final String result1 = DatabaseUtils.stringForQuery(db, "PRAGMA foreign_keys;", null); //$NON-NLS-1$
// Log.v(Constants.LOG_TAG, String.format("Foreign keys support result was: %s", result1)); //$NON-NLS-1$
// }
// catch (final SQLiteDoneException e)
// {
// Log.w(Constants.LOG_TAG, e);
// }
// }
}
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
@Override
public void onOpen (final SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys=ON;");
this.log.i("foreign_keys=ON");
}
}
private static boolean isDbSafe(SQLiteDatabase db) {
return db.isOpen() && !db.isReadOnly() && !db.isDbLockedByCurrentThread();
}
private static boolean isDbSafe(SQLiteDatabase db) {
return db.isOpen() && !db.isReadOnly() && !db.isDbLockedByCurrentThread();
}
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();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Upgrade queries
// Do not use static table and column names
if(oldVersion == 1) {
// Upgrade from 1 to version 2
// Add record length attribute
if (!db.isReadOnly()) {
db.execSQL("ALTER TABLE record ADD COLUMN time_length INTEGER");
}
oldVersion = 2;
}
if(oldVersion == 2) {
// Add gps speed and bearing attribute
if (!db.isReadOnly()) {
db.execSQL("ALTER TABLE leq ADD COLUMN speed FLOAT");
db.execSQL("ALTER TABLE leq ADD COLUMN bearing FLOAT");
}
oldVersion = 3;
}
if(oldVersion == 3) {
if(db.isReadOnly()) {
// New feature, user input
db.execSQL("ALTER TABLE leq ADD COLUMN description TEXT");
db.execSQL("ALTER TABLE leq ADD COLUMN pleasantness SMALLINT DEFAULT 2");
db.execSQL("ALTER TABLE leq ADD COLUMN photo_miniature BLOB");
db.execSQL("ALTER TABLE leq ADD COLUMN photo_uri TEXT");
db.execSQL( "CREATE TABLE record_tag(tag_id INTEGER PRIMARY KEY, record_id INTEGER, " +
"PRIMARY KEY(tag_id, record_id) " +
"FOREIGN KEY(record_id) REFERENCES record(record_id) ON DELETE CASCADE);");
}
oldVersion = 4;
}
if(oldVersion == 4) {
db.execSQL("ALTER TABLE record_tag ADD COLUMN tag_system_name TEXT");
oldVersion = 5;
}
if(oldVersion == 5) {
// Copy content to new table
db.execSQL("ALTER TABLE record rename to record_old;");
db.execSQL( "CREATE TABLE record(record_id INTEGER PRIMARY KEY, record_utc LONG," +
" upload_id TEXT, leq_mean FLOAT, time_length INTEGER, description TEXT," +
" photo_uri TEXT, pleasantness SMALLINT DEFAULT 2);");
db.execSQL("INSERT INTO record SELECT record_id , record_utc ,upload_id , leq_mean ," +
" time_length , description ,photo_uri , pleasantness from record_old;");
db.execSQL("DROP TABLE IF EXISTS record_old;");
oldVersion = 6;
}
if(oldVersion == 6) {
if (!db.isReadOnly()) {
db.execSQL("ALTER TABLE record ADD COLUMN calibration_gain FLOAT DEFAULT 0");
}
oldVersion = 7;
}
if(oldVersion == 7) {
if(!db.isReadOnly()) {
// Up to version 7, there was a swapping of speed and bearing
db.execSQL("UPDATE leq SET bearing=speed, speed=bearing;");
}
oldVersion = 8;
}
if(oldVersion == 8) {
if(!db.isReadOnly()) {
db.execSQL("ALTER TABLE record add column "+Record.COLUMN_NOISEPARTY_TAG + " TEXT");
}
oldVersion = 9;
}
}
public static void onOpen(SQLiteDatabase db) {
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
public static void onOpen(SQLiteDatabase db) {
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}