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) + "\">"; }
private static final void readExceptionFromParcel(Parcel reply, String msg, int code) { switch (code) { case 2: throw new IllegalArgumentException(msg); case 3: throw new UnsupportedOperationException(msg); case 4: throw new SQLiteAbortException(msg); case 5: throw new SQLiteConstraintException(msg); case 6: throw new SQLiteDatabaseCorruptException(msg); case 7: throw new SQLiteFullException(msg); case 8: throw new SQLiteDiskIOException(msg); case 9: throw new SQLiteException(msg); case 11: throw new OperationCanceledException(msg); default: reply.readException(code, msg); } }
private void onSqliteError(SQLiteException e) { if ( e instanceof SQLiteBindOrColumnIndexOutOfRangeException || e instanceof SQLiteConstraintException || e instanceof SQLiteDatabaseCorruptException || e instanceof SQLiteDatatypeMismatchException ) { // If a migration did not go well, the best we can do is drop the database and re-create // it from scratch. This is hackish but should allow more or less graceful recoveries. TrackHelper.track().event("Office", "cache.db.error").name("critical").value(1f).with(tracker); Log.e(TAG, "Critical database error. Droping + Re-creating", e); close(); ctx.deleteDatabase(DB_NAME); } else { // Generic error. Close + re-open Log.e(TAG, "Datable "+e.getClass().getName()+". Closing + re-opening", e); TrackHelper.track().event("Office", "cache.db.error").name(e.getClass().getName()).value(1f).with(tracker); close(); } }
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; } }
/** * Special function for writing an exception result at the header of * a parcel, to be used when returning an exception from a transaction. * exception will be re-thrown by the function in another process * * @param reply Parcel to write to * @param e The Exception to be written. * @see Parcel#writeNoException * @see Parcel#writeException */ public static final void writeExceptionToParcel(Parcel reply, Exception e) { int code = 0; boolean logException = true; if (e instanceof FileNotFoundException) { code = 1; logException = false; } else if (e instanceof IllegalArgumentException) { code = 2; } else if (e instanceof UnsupportedOperationException) { code = 3; } else if (e instanceof SQLiteAbortException) { code = 4; } else if (e instanceof SQLiteConstraintException) { code = 5; } else if (e instanceof SQLiteDatabaseCorruptException) { code = 6; } else if (e instanceof SQLiteFullException) { code = 7; } else if (e instanceof SQLiteDiskIOException) { code = 8; } else if (e instanceof SQLiteException) { code = 9; } else if (e instanceof OperationApplicationException) { code = 10; } else if (e instanceof OperationCanceledException) { code = 11; logException = false; } else { reply.writeException(e); Log.e(TAG, "Writing exception to parcel", e); return; } reply.writeInt(code); reply.writeString(e.getMessage()); if (logException) { Log.e(TAG, "Writing exception to parcel", e); } }
/** * Execute this SQL statement, if it is not a SELECT / INSERT / DELETE / UPDATE, for example * CREATE / DROP table, view, trigger, index etc. * * @throws SQLException If the SQL string is invalid for some reason */ @Override public void execute() { acquireReference(); try { getSession().execute(getSql(), getBindArgs(), getConnectionFlags(), null); } catch (SQLiteDatabaseCorruptException ex) { onCorruption(); throw ex; } finally { releaseReference(); } }
/** * Execute this SQL statement, if the the number of rows affected by execution of this SQL * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements. * * @return the number of rows affected by this SQL statement execution. * @throws SQLException If the SQL string is invalid for some reason */ @Override public int executeUpdateDelete() { acquireReference(); try { return getSession().executeForChangedRowCount( getSql(), getBindArgs(), getConnectionFlags(), null); } catch (SQLiteDatabaseCorruptException ex) { onCorruption(); throw ex; } finally { releaseReference(); } }
/** * Execute this SQL statement and return the ID of the row inserted due to this call. * The SQL statement should be an INSERT for this to be a useful call. * * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise. * * @throws SQLException If the SQL string is invalid for some reason */ @Override public long executeInsert() { acquireReference(); try { return getSession().executeForLastInsertedRowId( getSql(), getBindArgs(), getConnectionFlags(), null); } catch (SQLiteDatabaseCorruptException ex) { onCorruption(); throw ex; } finally { releaseReference(); } }
/** * Execute a statement that returns a 1 by 1 table with a numeric value. * For example, SELECT COUNT(*) FROM table; * * @return The result of the query. * * @throws SQLiteDoneException if the query returns zero rows */ @Override public long simpleQueryForLong() { acquireReference(); try { return getSession().executeForLong( getSql(), getBindArgs(), getConnectionFlags(), null); } catch (SQLiteDatabaseCorruptException ex) { onCorruption(); throw ex; } finally { releaseReference(); } }
/** * Execute a statement that returns a 1 by 1 table with a text value. * For example, SELECT COUNT(*) FROM table; * * @return The result of the query. * * @throws SQLiteDoneException if the query returns zero rows */ @Override public String simpleQueryForString() { acquireReference(); try { return getSession().executeForString( getSql(), getBindArgs(), getConnectionFlags(), null); } catch (SQLiteDatabaseCorruptException ex) { onCorruption(); throw ex; } finally { releaseReference(); } }
/** * Executes a statement that returns a 1 by 1 table with a blob value. * * @return A read-only file descriptor for a copy of the blob value, or {@code null} * if the value is null or could not be read for some reason. * * @throws SQLiteDoneException if the query returns zero rows */ public ParcelFileDescriptor simpleQueryForBlobFileDescriptor() { acquireReference(); try { return getSession().executeForBlobFileDescriptor( getSql(), getBindArgs(), getConnectionFlags(), null); } catch (SQLiteDatabaseCorruptException ex) { onCorruption(); throw ex; } finally { releaseReference(); } }
public static void throwSQLException(android.database.SQLException exception) throws SQLException { if(exception instanceof SQLiteConstraintException) { throw new SQLIntegrityConstraintViolationException(exception); } else if(exception instanceof SQLiteCantOpenDatabaseException || exception instanceof SQLiteDatabaseCorruptException || exception instanceof SQLiteAccessPermException) { throw new SQLNonTransientException(exception); } throw new SQLException(exception); }
/** * Returns {@link SQLiteDatabase} instance for the specified file path and use * the specified {@link CursorFactory} as the cursor. If the file path is * actually a file name, an absolute file path will be generated using * {@link Util#normalizeFilePath(android.content.Context, java.lang.String)} * method. * * @param filePath file name/path of database file * @param targetVersion target version for brand new databases * @param cursor to use with the database * @return {@link SQLiteDatabase} instance */ public SQLiteDatabase openDatabase(String filePath, int targetVersion, CursorFactory cursor) { // Normalize the file path if application is known. filePath = Util.normalizeFilePath(getApplication(), filePath); // Try to find a cached instance. SQLiteDatabase db = databases.get(filePath); // If this is the first time for getting this database, or if the database // was previously closed by the user. if(null == db || !db.isOpen()) { // Auto-create parent directories. File parentFile = new File(Util.getParentPath(filePath)); // If parent directory is actually a file, throw an error. if(parentFile.isFile()) { throw new IllegalStateException( "Parent directory is actually a file: " + parentFile); } // Auto-create parent directories. if(!parentFile.mkdirs() && !parentFile.isDirectory()) { throw new IllegalStateException( "Unable to create parent directories for database: " + filePath); } try { // Try to open the database first. db = SQLiteDatabase.openDatabase( filePath, cursor, SQLiteDatabase.OPEN_READWRITE ); // Log the opening. Log.i(LOG_TAG, "Opened database (v" + db.getVersion() + "): " + filePath); } catch(SQLiteDatabaseCorruptException error) { Log.e(LOG_TAG, "Corruption error detected in database: " + filePath); throw error; } catch(SQLiteException ignored) { // The database is possibly non-existent, create it. db = SQLiteDatabase.openOrCreateDatabase(filePath, cursor); // Database created; set initial version. db.setVersion(targetVersion); // Log the creation. Log.i(LOG_TAG, "Created database (v1): " + filePath); } // Store in internal dictionary. databases.put(filePath, db); } return db; }
private SQLiteDatabase openDB(File sqlitefile) throws SQLiteDatabaseCorruptException { SQLiteDatabase sqlh=SQLiteDatabase.openDatabase(sqlitefile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS ); return sqlh; }