Java Code Examples for android.database.sqlite.SQLiteDatabase#isReadOnly()

The following examples show how to use android.database.sqlite.SQLiteDatabase#isReadOnly() . 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: DataProvider.java    From callmeter with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: Storage.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL(ACTIVATE_FOREIGN_KEY);
    }
}
 
Example 3
Source File: DbAdapter.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
@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");
	}
}
 
Example 4
Source File: PacketDatabaseHelper.java    From sniffer154 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onOpen(SQLiteDatabase db) {
	super.onOpen(db);
	if (!db.isReadOnly()) {
		// Enable foreign key constraints
		db.execSQL("PRAGMA foreign_keys=ON;");
	}
}
 
Example 5
Source File: WifiAuthDatabase.java    From WiFiAfterConnect with Apache License 2.0 5 votes vote down vote up
@Override
public void onOpen(SQLiteDatabase db) {
	super.onOpen(db);
	if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL("PRAGMA foreign_keys=ON;");
    }		
}
 
Example 6
Source File: LocalyticsProvider.java    From ExtensionsPack with MIT License 5 votes vote down vote up
@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);
        // }
        // }
    }
}
 
Example 7
Source File: LocalyticsProvider.java    From Beats with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
        // }
        // }
    }
}
 
Example 8
Source File: CPOrmDatabase.java    From CPOrm with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onConfigure(SQLiteDatabase db) {

    super.onConfigure(db);
    if (!db.isReadOnly()) {
        db.enableWriteAheadLogging();
    }
}
 
Example 9
Source File: DbHelper.java    From Material-Movies with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase db) {
    if (db.isReadOnly()) {
        db = getWritableDatabase();
    }
    db.execSQL(DbConstants.Movies.CREATE_SQL);
}
 
Example 10
Source File: QuantumFluxDatabase.java    From QuantumFlux with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onConfigure(SQLiteDatabase db) {
    super.onConfigure(db);
    if (!db.isReadOnly()) {
        db.enableWriteAheadLogging();
    }
}
 
Example 11
Source File: KeyValDatabase.java    From GodotSQL with Apache License 2.0 5 votes vote down vote up
@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);
		}
 
Example 12
Source File: Storage.java    From NoiseCapture with GNU General Public License v3.0 4 votes vote down vote up
@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;
    }
}
 
Example 13
Source File: Service.java    From Twire with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isDbSafe(SQLiteDatabase db) {
    return db.isOpen() && !db.isReadOnly() && !db.isDbLockedByCurrentThread();
}
 
Example 14
Source File: DbHelperUtils.java    From q-municate-android with Apache License 2.0 4 votes vote down vote up
public static void onOpen(SQLiteDatabase db) {
    if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}
 
Example 15
Source File: DbHelperUtils.java    From q-municate-android with Apache License 2.0 4 votes vote down vote up
public static void onOpen(SQLiteDatabase db) {
    if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}
 
Example 16
Source File: SQLiteSDCardHelper.java    From android-sdcard-helper with Apache License 2.0 4 votes vote down vote up
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 17
Source File: Service.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isDbSafe(SQLiteDatabase db) {
    return db.isOpen() && !db.isReadOnly() && !db.isDbLockedByCurrentThread();
}