0

I need re write this sqlite Statements from java to c++, BEcouse of low performance: I red this arcticle:

Improve INSERT-per-second performance of SQLite?

But Iam totaly confused, becouse I cant find any sqlstatemenst of Insert or Update

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

IN Java:

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

// THis can be same in the c++? like

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

ANd rest is the clear for me plus minus // it

    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();
    }
}
Community
  • 1
  • 1
user2165656
  • 445
  • 1
  • 3
  • 11

2 Answers2

1

You can use std::to_string to build a string using your variables

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

If any of your variables are already std::string, then you don't need to use this function you can simply use + to concatenate.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

is this ok?

      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] = "\0";

//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);

}

user2165656
  • 445
  • 1
  • 3
  • 11