Search in sources :

Example 91 with SQLiteException

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;
}
Also used : Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 92 with SQLiteException

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());
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FileWriter(java.io.FileWriter) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) SQLiteException(android.database.sqlite.SQLiteException) BufferedWriter(java.io.BufferedWriter)

Example 93 with SQLiteException

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();
        }
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) File(java.io.File) SQLiteException(android.database.sqlite.SQLiteException)

Example 94 with SQLiteException

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();
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException) Test(org.junit.Test)

Example 95 with SQLiteException

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());
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FileWriter(java.io.FileWriter) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) SQLiteException(android.database.sqlite.SQLiteException) BufferedWriter(java.io.BufferedWriter)

Aggregations

SQLiteException (android.database.sqlite.SQLiteException)122 Cursor (android.database.Cursor)72 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)36 ContentValues (android.content.ContentValues)28 SQLException (android.database.SQLException)17 Intent (android.content.Intent)14 HandlerThread (android.os.HandlerThread)10 File (java.io.File)10 HashMap (java.util.HashMap)8 Account (android.accounts.Account)7 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)7 SyncStatusInfo (android.content.SyncStatusInfo)6 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)6 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 Uri (android.net.Uri)5 ArrayList (java.util.ArrayList)5 SuppressLint (android.annotation.SuppressLint)4 SQLiteStatement (android.database.sqlite.SQLiteStatement)3 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)3