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);
}
}
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();
}
}
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);
}
}
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();
}
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();
}
Aggregations