Search in sources :

Example 76 with SQLiteException

use of android.database.sqlite.SQLiteException in project requery by requery.

the class SqlitexStatement method executeUpdate.

@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
    SQLiteStatement statement = null;
    try {
        statement = connection.getDatabase().compileStatement(sql);
        if (autoGeneratedKeys == RETURN_GENERATED_KEYS) {
            long rowId = statement.executeInsert();
            insertResult = new SingleResultSet(this, rowId);
            updateCount = 1;
        } else {
            updateCount = statement.executeUpdateDelete();
        }
    } catch (SQLiteException e) {
        SqlitexConnection.throwSQLException(e);
    } finally {
        if (statement != null) {
            statement.close();
        }
    }
    return updateCount;
}
Also used : SQLiteStatement(io.requery.android.database.sqlite.SQLiteStatement) SingleResultSet(io.requery.android.sqlite.SingleResultSet) SQLiteException(android.database.sqlite.SQLiteException)

Example 77 with SQLiteException

use of android.database.sqlite.SQLiteException in project android_frameworks_base by ParanoidAndroid.

the class SettingsProvider method lookupValue.

// Looks up value 'key' in 'table' and returns either a single-pair Bundle,
// possibly with a null value, or null on failure.
private Bundle lookupValue(DatabaseHelper dbHelper, String table, final SettingsCache cache, String key) {
    if (cache == null) {
        Slog.e(TAG, "cache is null for user " + UserHandle.getCallingUserId() + " : key=" + key);
        return null;
    }
    synchronized (cache) {
        Bundle value = cache.get(key);
        if (value != null) {
            if (value != TOO_LARGE_TO_CACHE_MARKER) {
                return value;
            }
        // else we fall through and read the value from disk
        } else if (cache.fullyMatchesDisk()) {
            // from somebody's UI thread...
            return NULL_SETTING;
        }
    }
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.query(table, COLUMN_VALUE, "name=?", new String[] { key }, null, null, null, null);
        if (cursor != null && cursor.getCount() == 1) {
            cursor.moveToFirst();
            return cache.putIfAbsent(key, cursor.getString(0));
        }
    } catch (SQLiteException e) {
        Log.w(TAG, "settings lookup error", e);
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
    cache.putIfAbsent(key, null);
    return NULL_SETTING;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Bundle(android.os.Bundle) AbstractCursor(android.database.AbstractCursor) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 78 with SQLiteException

use of android.database.sqlite.SQLiteException in project android_frameworks_base by ParanoidAndroid.

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 79 with SQLiteException

use of android.database.sqlite.SQLiteException in project platform_frameworks_base by android.

the class DrmManagerClient method convertUriToPath.

/**
     * This method expects uri in the following format
     *     content://media/<table_name>/<row_index> (or)
     *     file://sdcard/test.mp4
     *     http://test.com/test.mp4
     *
     * Here <table_name> shall be "video" or "audio" or "images"
     * <row_index> the index of the content in given table
     */
private String convertUriToPath(Uri uri) {
    String path = null;
    if (null != uri) {
        String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();
        } else if (scheme.equals("http")) {
            path = uri.toString();
        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found" + " in media store");
                }
                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (SQLiteException e) {
                throw new IllegalArgumentException("Given Uri is not formatted in a way " + "so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }
    return path;
}
Also used : Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 80 with SQLiteException

use of android.database.sqlite.SQLiteException in project DBFlow by Raizlabs.

the class BaseDatabaseHelper method executeCreations.

/**
     * This method executes CREATE TABLE statements as well as CREATE VIEW on the database passed.
     */
protected void executeCreations(final DatabaseWrapper database) {
    try {
        database.beginTransaction();
        List<ModelAdapter> modelAdapters = databaseDefinition.getModelAdapters();
        for (ModelAdapter modelAdapter : modelAdapters) {
            try {
                database.execSQL(modelAdapter.getCreationQuery());
            } catch (SQLiteException e) {
                FlowLog.logError(e);
            }
        }
        // create our model views
        List<ModelViewAdapter> modelViews = databaseDefinition.getModelViewAdapters();
        for (ModelViewAdapter modelView : modelViews) {
            QueryBuilder queryBuilder = new QueryBuilder().append("CREATE VIEW IF NOT EXISTS").appendSpaceSeparated(modelView.getViewName()).append("AS ").append(modelView.getCreationQuery());
            try {
                database.execSQL(queryBuilder.getQuery());
            } catch (SQLiteException e) {
                FlowLog.logError(e);
            }
        }
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
Also used : ModelAdapter(com.raizlabs.android.dbflow.structure.ModelAdapter) QueryBuilder(com.raizlabs.android.dbflow.sql.QueryBuilder) SQLiteException(android.database.sqlite.SQLiteException) ModelViewAdapter(com.raizlabs.android.dbflow.structure.ModelViewAdapter)

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