Search in sources :

Example 1 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project Signal-Android by WhisperSystems.

the class PlaintextBackupImporter method importPlaintext.

private static void importPlaintext(Context context, MasterSecret masterSecret) throws IOException {
    Log.w("PlaintextBackupImporter", "importPlaintext()");
    SmsDatabase db = DatabaseFactory.getSmsDatabase(context);
    SQLiteDatabase transaction = db.beginTransaction();
    try {
        ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
        XmlBackup backup = new XmlBackup(getPlaintextExportFile().getAbsolutePath());
        MasterCipher masterCipher = new MasterCipher(masterSecret);
        Set<Long> modifiedThreads = new HashSet<Long>();
        XmlBackup.XmlBackupItem item;
        while ((item = backup.getNext()) != null) {
            Recipients recipients = RecipientFactory.getRecipientsFromString(context, item.getAddress(), false);
            long threadId = threads.getThreadIdFor(recipients);
            SQLiteStatement statement = db.createInsertStatement(transaction);
            if (item.getAddress() == null || item.getAddress().equals("null"))
                continue;
            if (!isAppropriateTypeForImport(item.getType()))
                continue;
            addStringToStatement(statement, 1, item.getAddress());
            addNullToStatement(statement, 2);
            addLongToStatement(statement, 3, item.getDate());
            addLongToStatement(statement, 4, item.getDate());
            addLongToStatement(statement, 5, item.getProtocol());
            addLongToStatement(statement, 6, item.getRead());
            addLongToStatement(statement, 7, item.getStatus());
            addTranslatedTypeToStatement(statement, 8, item.getType());
            addNullToStatement(statement, 9);
            addStringToStatement(statement, 10, item.getSubject());
            addEncryptedStingToStatement(masterCipher, statement, 11, item.getBody());
            addStringToStatement(statement, 12, item.getServiceCenter());
            addLongToStatement(statement, 13, threadId);
            modifiedThreads.add(threadId);
            statement.execute();
        }
        for (long threadId : modifiedThreads) {
            threads.update(threadId, true);
        }
        Log.w("PlaintextBackupImporter", "Exited loop");
    } catch (XmlPullParserException e) {
        Log.w("PlaintextBackupImporter", e);
        throw new IOException("XML Parsing error!");
    } finally {
        db.endTransaction(transaction);
    }
}
Also used : Recipients(org.thoughtcrime.securesms.recipients.Recipients) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) IOException(java.io.IOException) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) HashSet(java.util.HashSet)

Example 2 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement 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();
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException) Uri(android.net.Uri)

Example 3 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project ormlite-android by j256.

the class AndroidDatabaseConnection method insert.

@Override
public int insert(String statement, Object[] args, FieldType[] argFieldTypes, GeneratedKeyHolder keyHolder) throws SQLException {
    SQLiteStatement stmt = null;
    try {
        stmt = db.compileStatement(statement);
        bindArgs(stmt, args, argFieldTypes);
        long rowId = stmt.executeInsert();
        if (keyHolder != null) {
            keyHolder.addKey(rowId);
        }
        /*
			 * I've decided to not do the CHANGES() statement here like we do down below in UPDATE because we know that
			 * it worked (since it didn't throw) so we know that 1 is right.
			 */
        int result = 1;
        logger.trace("{}: insert statement is compiled and executed, changed {}: {}", this, result, statement);
        return result;
    } catch (android.database.SQLException e) {
        throw SqlExceptionUtil.create("inserting to database failed: " + statement, e);
    } finally {
        closeQuietly(stmt);
    }
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) Savepoint(java.sql.Savepoint)

Example 4 with SQLiteStatement

use of android.database.sqlite.SQLiteStatement in project k-9 by k9mail.

the class Storage method put.

void put(Map<String, String> insertables) {
    String sql = "INSERT INTO preferences_storage (primkey, value) VALUES (?, ?)";
    SQLiteStatement stmt = workingDB.get().compileStatement(sql);
    for (Map.Entry<String, String> entry : insertables.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        stmt.bindString(1, key);
        stmt.bindString(2, value);
        stmt.execute();
        stmt.clearBindings();
        liveUpdate(key, value);
    }
    stmt.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 5 with SQLiteStatement

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

the class DatabaseStatementTest method testSimpleQuery.

@MediumTest
public void testSimpleQuery() throws Exception {
    mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);");
    mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');");
    SQLiteStatement statement1 = mDatabase.compileStatement("SELECT num FROM test WHERE str = ?");
    SQLiteStatement statement2 = mDatabase.compileStatement("SELECT str FROM test WHERE num = ?");
    try {
        statement1.bindString(1, "hello");
        long value = statement1.simpleQueryForLong();
        assertEquals(1234, value);
        statement1.bindString(1, "world");
        statement1.simpleQueryForLong();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    try {
        statement2.bindLong(1, 1234);
        String value = statement1.simpleQueryForString();
        assertEquals("hello", value);
        statement2.bindLong(1, 5678);
        statement1.simpleQueryForString();
        fail("shouldn't get here");
    } catch (SQLiteDoneException e) {
    // expected
    }
    statement1.close();
    statement2.close();
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLiteDoneException(android.database.sqlite.SQLiteDoneException) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Aggregations

SQLiteStatement (android.database.sqlite.SQLiteStatement)249 Cursor (android.database.Cursor)62 MediumTest (android.test.suitebuilder.annotation.MediumTest)49 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)24 SQLException (android.database.SQLException)21 Test (org.junit.Test)20 Date (java.util.Date)12 TargetApi (android.annotation.TargetApi)8 SQLiteDoneException (android.database.sqlite.SQLiteDoneException)8 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)7 SdkSuppress (android.support.test.filters.SdkSuppress)7 ArrayList (java.util.ArrayList)5 SQLiteException (android.database.sqlite.SQLiteException)4 SQLiteFullException (android.database.sqlite.SQLiteFullException)4 Timing (com.newsrob.util.Timing)3 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)3 IOException (java.io.IOException)3 ContentValues (android.content.ContentValues)2 VisibleForTesting (android.support.annotation.VisibleForTesting)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2