use of android.database.sqlite.SQLiteException in project 360-Engine-for-Android by 360.
the class ContactsTable method fetchUserIdFromLocalContactId.
public static long fetchUserIdFromLocalContactId(Long localContactId, SQLiteDatabase readableDb) {
DatabaseHelper.trace(false, "ContactsTable.fetchSyncToPhone()");
long userId = -1;
Cursor c = null;
try {
c = readableDb.rawQuery("SELECT " + Field.USERID + " FROM " + TABLE_NAME + " WHERE " + Field.LOCALID + "=" + localContactId, null);
while (c.moveToNext()) {
if (!c.isNull(0)) {
userId = c.getLong(0);
}
}
} catch (SQLiteException e) {
LogUtils.logE("ContactsTable.fetchSyncToPhone() Exception - Unable to run query:\n", e);
} finally {
CloseUtils.close(c);
}
return userId;
}
use of android.database.sqlite.SQLiteException in project android_frameworks_base by DirtyUnicorns.
the class DatabaseErrorHandlerTest method testDatabaseIsCorrupt.
public void testDatabaseIsCorrupt() throws IOException {
mDatabase.execSQL("create table t (i int);");
// write junk into the database file
BufferedWriter writer = new BufferedWriter(new FileWriter(mDatabaseFile.getPath()));
writer.write("blah");
writer.close();
assertTrue(mDatabaseFile.exists());
// should trigger call to MyDatabaseCorruptionHandler.onCorruption
try {
mDatabase.execSQL("select * from t;");
fail("expected exception");
} catch (SQLiteDiskIOException e) {
// expected
if (mDatabaseFile.exists()) {
mDatabaseFile.delete();
}
} catch (SQLiteException e) {
}
// database file should be gone
assertFalse(mDatabaseFile.exists());
// after corruption handler is called, the database file should be free of
// database corruption
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null, new MyDatabaseCorruptionHandler());
assertTrue(db.isDatabaseIntegrityOk());
}
use of android.database.sqlite.SQLiteException in project newsrob by marianokamp.
the class SQLiteOpenHelper method getWritableDatabase.
/**
* 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()) {
// 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;
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
if (mPath != null)
db = SQLiteDatabase.openOrCreateDatabase(new File(mPath, mName), mFactory);
else
db = mContext.openOrCreateDatabase(mName, 0, mFactory);
}
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();
}
}
}
use of android.database.sqlite.SQLiteException in project robolectric by robolectric.
the class SQLiteDatabaseTest method testCreateAndDropTable.
@Test
public void testCreateAndDropTable() throws Exception {
SQLiteDatabase db = openOrCreateDatabase("db1");
db.execSQL("CREATE TABLE foo(id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);");
Cursor c = db.query("FOO", null, null, null, null, null, null);
assertThat(c).isNotNull();
c.close();
db.close();
db = openOrCreateDatabase("db1");
db.execSQL("DROP TABLE IF EXISTS foo;");
try {
c = db.query("FOO", null, null, null, null, null, null);
fail("expected no such table exception");
} catch (SQLiteException e) {
// TODO
}
db.close();
}
use of android.database.sqlite.SQLiteException in project android_frameworks_base by ResurrectionRemix.
the class DatabaseErrorHandlerTest method testDatabaseIsCorrupt.
public void testDatabaseIsCorrupt() throws IOException {
mDatabase.execSQL("create table t (i int);");
// write junk into the database file
BufferedWriter writer = new BufferedWriter(new FileWriter(mDatabaseFile.getPath()));
writer.write("blah");
writer.close();
assertTrue(mDatabaseFile.exists());
// should trigger call to MyDatabaseCorruptionHandler.onCorruption
try {
mDatabase.execSQL("select * from t;");
fail("expected exception");
} catch (SQLiteDiskIOException e) {
// expected
if (mDatabaseFile.exists()) {
mDatabaseFile.delete();
}
} catch (SQLiteException e) {
}
// database file should be gone
assertFalse(mDatabaseFile.exists());
// after corruption handler is called, the database file should be free of
// database corruption
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null, new MyDatabaseCorruptionHandler());
assertTrue(db.isDatabaseIntegrityOk());
}
Aggregations