Search in sources :

Example 21 with SQLiteException

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

the class SqlitexPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    try {
        String[] args = bindingsToArray();
        cursor = connection.getDatabase().rawQuery(getSql(), args);
        return queryResult = new CursorResultSet(this, cursor, false);
    } catch (SQLiteException e) {
        SqlitexConnection.throwSQLException(e);
    }
    return null;
}
Also used : CursorResultSet(io.requery.android.sqlite.CursorResultSet) SQLiteException(android.database.sqlite.SQLiteException)

Example 22 with SQLiteException

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

the class SqlitexPreparedStatement method executeUpdate.

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

Example 23 with SQLiteException

use of android.database.sqlite.SQLiteException in project sugar by satyan.

the class SugarRecord method count.

public static <T> long count(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
    long result = -1;
    String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : "";
    SQLiteStatement sqliteStatement;
    try {
        sqliteStatement = getSugarDataBase().compileStatement("SELECT count(*) FROM " + NamingHelper.toTableName(type) + filter);
    } catch (SQLiteException e) {
        e.printStackTrace();
        return result;
    }
    if (whereArgs != null) {
        for (int i = whereArgs.length; i != 0; i--) {
            sqliteStatement.bindString(i, whereArgs[i - 1]);
        }
    }
    try {
        result = sqliteStatement.simpleQueryForLong();
    } finally {
        sqliteStatement.close();
    }
    return result;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteException(android.database.sqlite.SQLiteException)

Example 24 with SQLiteException

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

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

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

the class SyncStorageEngine method readAndDeleteLegacyAccountInfoLocked.

/**
     * Load sync engine state from the old syncmanager database, and then
     * erase it.  Note that we don't deal with pending operations, active
     * sync, or history.
     */
private void readAndDeleteLegacyAccountInfoLocked() {
    // Look for old database to initialize from.
    File file = mContext.getDatabasePath("syncmanager.db");
    if (!file.exists()) {
        return;
    }
    String path = file.getPath();
    SQLiteDatabase db = null;
    try {
        db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
    }
    if (db != null) {
        final boolean hasType = db.getVersion() >= 11;
        // Copy in all of the status information, as well as accounts.
        if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
            Slog.v(TAG_FILE, "Reading legacy sync accounts db");
        }
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables("stats, status");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("_id", "status._id as _id");
        map.put("account", "stats.account as account");
        if (hasType) {
            map.put("account_type", "stats.account_type as account_type");
        }
        map.put("authority", "stats.authority as authority");
        map.put("totalElapsedTime", "totalElapsedTime");
        map.put("numSyncs", "numSyncs");
        map.put("numSourceLocal", "numSourceLocal");
        map.put("numSourcePoll", "numSourcePoll");
        map.put("numSourceServer", "numSourceServer");
        map.put("numSourceUser", "numSourceUser");
        map.put("lastSuccessSource", "lastSuccessSource");
        map.put("lastSuccessTime", "lastSuccessTime");
        map.put("lastFailureSource", "lastFailureSource");
        map.put("lastFailureTime", "lastFailureTime");
        map.put("lastFailureMesg", "lastFailureMesg");
        map.put("pending", "pending");
        qb.setProjectionMap(map);
        qb.appendWhere("stats._id = status.stats_id");
        Cursor c = qb.query(db, null, null, null, null, null, null);
        while (c.moveToNext()) {
            String accountName = c.getString(c.getColumnIndex("account"));
            String accountType = hasType ? c.getString(c.getColumnIndex("account_type")) : null;
            if (accountType == null) {
                accountType = "com.google";
            }
            String authorityName = c.getString(c.getColumnIndex("authority"));
            AuthorityInfo authority = this.getOrCreateAuthorityLocked(new EndPoint(new Account(accountName, accountType), authorityName, 0), -1, false);
            if (authority != null) {
                int i = mSyncStatus.size();
                boolean found = false;
                SyncStatusInfo st = null;
                while (i > 0) {
                    i--;
                    st = mSyncStatus.valueAt(i);
                    if (st.authorityId == authority.ident) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    st = new SyncStatusInfo(authority.ident);
                    mSyncStatus.put(authority.ident, st);
                }
                st.totalElapsedTime = getLongColumn(c, "totalElapsedTime");
                st.numSyncs = getIntColumn(c, "numSyncs");
                st.numSourceLocal = getIntColumn(c, "numSourceLocal");
                st.numSourcePoll = getIntColumn(c, "numSourcePoll");
                st.numSourceServer = getIntColumn(c, "numSourceServer");
                st.numSourceUser = getIntColumn(c, "numSourceUser");
                st.numSourcePeriodic = 0;
                st.lastSuccessSource = getIntColumn(c, "lastSuccessSource");
                st.lastSuccessTime = getLongColumn(c, "lastSuccessTime");
                st.lastFailureSource = getIntColumn(c, "lastFailureSource");
                st.lastFailureTime = getLongColumn(c, "lastFailureTime");
                st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg"));
                st.pending = getIntColumn(c, "pending") != 0;
            }
        }
        c.close();
        // Retrieve the settings.
        qb = new SQLiteQueryBuilder();
        qb.setTables("settings");
        c = qb.query(db, null, null, null, null, null, null);
        while (c.moveToNext()) {
            String name = c.getString(c.getColumnIndex("name"));
            String value = c.getString(c.getColumnIndex("value"));
            if (name == null)
                continue;
            if (name.equals("listen_for_tickles")) {
                setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0);
            } else if (name.startsWith("sync_provider_")) {
                String provider = name.substring("sync_provider_".length(), name.length());
                int i = mAuthorities.size();
                while (i > 0) {
                    i--;
                    AuthorityInfo authority = mAuthorities.valueAt(i);
                    if (authority.target.provider.equals(provider)) {
                        authority.enabled = value == null || Boolean.parseBoolean(value);
                        authority.syncable = 1;
                    }
                }
            }
        }
        c.close();
        db.close();
        (new File(path)).delete();
    }
}
Also used : Account(android.accounts.Account) HashMap(java.util.HashMap) SyncStatusInfo(android.content.SyncStatusInfo) SQLiteException(android.database.sqlite.SQLiteException) Cursor(android.database.Cursor) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) File(java.io.File) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Aggregations

SQLiteException (android.database.sqlite.SQLiteException)100 Cursor (android.database.Cursor)64 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)33 ContentValues (android.content.ContentValues)10 File (java.io.File)9 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 SuppressLint (android.annotation.SuppressLint)4 ArrayList (java.util.ArrayList)4 SQLiteStatement (android.database.sqlite.SQLiteStatement)3 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)3 SingleResultSet (io.requery.android.sqlite.SingleResultSet)3 Date (java.util.Date)3 SQLiteDoneException (android.database.sqlite.SQLiteDoneException)2