use of android.database.sqlite.SQLiteException in project NimbusBase_Android_Tutorial by NimbusBase.
the class PGFragmentRecordExist method onViewCreated.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null && mRecord != null)
actionBar.setTitle(mRecord.getTitle());
final SQLiteDatabase database = mSQLiteOpenHelper.getReadableDatabase();
try {
final Cursor cursor = database.query(mTableName, null, "_ROWID_ == ?", new String[] { mRecordID.toString() }, null, null, null);
if (cursor.moveToFirst()) {
final PGRecordBasic record = new PGRecordBasic(cursor);
if (!record.equals(mRecord)) {
this.mRecord = record;
reload(mAttrTypesByName, record.getValuesByAttrName());
if (actionBar != null)
actionBar.setTitle(record.getTitle());
}
}
} catch (SQLiteException e) {
} finally {
database.close();
}
}
use of android.database.sqlite.SQLiteException in project Signal-Android by WhisperSystems.
the class SmsMigrator method migrateConversation.
private static void migrateConversation(Context context, MasterSecret masterSecret, SmsMigrationProgressListener listener, ProgressDescription progress, long theirThreadId, long ourThreadId) {
SmsDatabase ourSmsDatabase = DatabaseFactory.getSmsDatabase(context);
Cursor cursor = null;
try {
Uri uri = Uri.parse("content://sms/conversations/" + theirThreadId);
try {
cursor = context.getContentResolver().query(uri, null, null, null, null);
} catch (SQLiteException e) {
/// Work around for weird sony-specific (?) bug: #4309
Log.w(TAG, e);
return;
}
SQLiteDatabase transaction = ourSmsDatabase.beginTransaction();
SQLiteStatement statement = ourSmsDatabase.createInsertStatement(transaction);
while (cursor != null && cursor.moveToNext()) {
int typeColumn = cursor.getColumnIndex(SmsDatabase.TYPE);
if (cursor.isNull(typeColumn) || isAppropriateTypeForMigration(cursor, typeColumn)) {
getContentValuesForRow(context, masterSecret, cursor, ourThreadId, statement);
statement.execute();
}
listener.progressUpdate(new ProgressDescription(progress, cursor.getCount(), cursor.getPosition()));
}
ourSmsDatabase.endTransaction(transaction);
DatabaseFactory.getThreadDatabase(context).update(ourThreadId, true);
DatabaseFactory.getThreadDatabase(context).notifyConversationListeners(ourThreadId);
} finally {
if (cursor != null)
cursor.close();
}
}
use of android.database.sqlite.SQLiteException in project android-sqlite-asset-helper by jgilfelt.
the class SQLiteAssetHelper method getReadableDatabase.
/**
* 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()) {
// The database is already open for business
return mDatabase;
}
if (mIsInitializing) {
throw new IllegalStateException("getReadableDatabase called recursively");
}
try {
return getWritableDatabase();
} catch (SQLiteException e) {
// Can't open a temp database read-only!
if (mName == null)
throw e;
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();
}
}
use of android.database.sqlite.SQLiteException in project android-sqlite-asset-helper by jgilfelt.
the class SQLiteAssetHelper method getWritableDatabase.
/**
* 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()) {
// The database is already open for business
return mDatabase;
}
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();
}
}
}
use of android.database.sqlite.SQLiteException in project zxingfragmentlib by mitoyarzun.
the class HistoryManager method trimHistory.
public void trimHistory() {
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
cursor.move(MAX_ITEMS);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
Log.i(TAG, "Deleting scan history ID " + id);
db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
}
} catch (SQLiteException sqle) {
// We're seeing an error here when called in CaptureActivity.onCreate() in rare cases
// and don't understand it. First theory is that it's transient so can be safely ignored.
Log.w(TAG, sqle);
// continue
} finally {
close(cursor, db);
}
}
Aggregations