re write-sql 语句 Insert OR REPLACE from java to c++ NDK Android

re write-sql statement Insert OR REPLACE from java to c++ NDK Android

我需要将这个 sqlite 语句从 java 重新写到 c++,因为性能低下: 我红了这篇文章:

Improve INSERT-per-second performance of SQLite?

但是我很困惑,因为我找不到插入或更新的任何 sqlstatemenst

 public synchronized void saveMatchValue(int photoRecOwner, int[] photoRecAssign, float[] value) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    database.beginTransaction();

输入Java:

      String sql = " INSERT OR REPLACE INTO " + TypeContract.CTablePhotoMatch.TABLE_NAME + "("
            + TypeContract.CTablePhotoMatch.FK_OWNER + "," + TypeContract.CTablePhotoMatch.FK_ASSIGN + ","
            + TypeContract.CTablePhotoMatch.VALUE + ") VALUES (?, ?, ?) ;";

// 这在 C++ 中可以相同吗?喜欢

         string sqlstatement = "INSERT INTO abe_account ("...........

剩下的对我来说很清楚加减//它

    SQLiteStatement stmt = database.compileStatement(sql);


    // stmt.bindDouble(index, value);
    // database.compileStatement(sql)
    try {
        String[] whereArgs = new String[2];
        int rows = 0;
        for (int i = 0; i < photoRecAssign.length; i++) {

            if (photoRecOwner > photoRecAssign[i]) {
                stmt.bindDouble(1, photoRecOwner);
                stmt.bindDouble(2, photoRecAssign[i]);


            } else {
                stmt.bindDouble(1, photoRecAssign[i]);
                stmt.bindDouble(2, photoRecOwner);


            }
            stmt.bindDouble(3, value[i]);

            try {
                long entryID = stmt.executeInsert();
            } catch (Exception e) {
                // updtStmt.executeUpdateDelete();
            } finally {
                stmt.clearBindings();

            }
            // ContentValues contentValues = crossTableContentValues(
            // photoRecOwner, photoRecAssign[i], value[i]);
            // database.insert(TypeContract.CTablePhotoMatch.TABLE_NAME,
            // null,
            // contentValues);
        }
        database.setTransactionSuccessful();
    } finally {
        stmt.close();

        database.endTransaction();
        // database.close();
    }
}

您可以使用 std::to_string 使用您的变量构建字符串

#include <string>
std::string sql = " INSERT OR REPLACE INTO " + std::to_string(TypeContract.CTablePhotoMatch.TABLE_NAME) + "(" + ...;

如果您的任何变量已经是 std::string,那么您不需要使用此函数,只需使用 + 进行连接即可。

这样可以吗?

      void NativeSaveMAValues(int size,int photoRecOwner,int photoRecAssign[],double value[]){

sqlite3 *db;
sqlite3_stmt * stmt;

char * sErrMsg = 0;
char * tail = 0;
int nRetCode;
char sSQL [BUFFER_SIZE] = "[=10=]";

//sqlite3_prepare_v2(db,  sSQL, BUFFER_SIZE, &stmt, &tail);
std::string sqlstatement =  std::string(" INSERT OR REPLACE INTO CrossPhotoMatchTable") + "("
        + "fk_owner" + ","
        + "fk_assign" + ","
        + "value" + ") VALUES (?, ?, ?) ;";

sqlite3_prepare( db, sqlstatement.c_str(), -1, &stmt, NULL );
sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &sErrMsg);

for (int i = 0; i < size; i++) {

    if (photoRecOwner > photoRecAssign[i]) {

        sqlite3_bind_int(stmt, 1, photoRecOwner);
        sqlite3_bind_int(stmt, 2, photoRecAssign[i]);
               // stmt.bindDouble(1, photoRecOwner);


    } else {
        sqlite3_bind_int(stmt, 1, photoRecAssign[i]);
        sqlite3_bind_int(stmt, 2, photoRecOwner);

    }

    sqlite3_bind_double(stmt, 3, value[i]);


    try {

        sqlite3_step(stmt);//todo isDONE?:

        sqlite3_clear_bindings(stmt);
        sqlite3_reset(stmt);

    } catch (std::exception e) {             // updtStmt.executeUpdateDelete();


    }

}

sqlite3_exec(db, "END TRANSACTION", NULL, NULL, &sErrMsg);
sqlite3_finalize(stmt);

}