Android SQLite中的事务和预处理语句

时间:2017-07-06 17:06:25

标签: android sqlite transactions android-sqlite prepared-statement

我正在使用Android 5.1和SQLite数据库。我有以下准备好的陈述和交易:

String stringValue = "hello";
try {

    String sql = "INSERT INTO table_name (column_1, column_2) VALUES (?, ?)";
    SQLiteStatement statement = db.compileStatement(sql);
    db.beginTransaction();

    for (int i = 0; i < 1000; i++) {
        statement.clearBindings();
        statement.bindLong(1, i);
        statement.bindString(2, stringValue + i);
        statement.executeInsert();
    }

    db.setTransactionSuccessful(); // This commits the transaction if there were no exceptions

} catch (Exception e) {
    Log.w("Exception:", e);
} finally {
    db.endTransaction();
}

这不起作用。不起作用意味着statement.executeInsert();阻塞,即没有异常或错误,但它只是永远停滞。当我第一次启动事务然后编译准备好的语句时,它就可以工作,即执行statement.executeInsert();并返回非常快。

为什么这不起作用,并且不可能只编译一次准备好的语句而不是每次交易。

0 个答案:

没有答案