使用多个 '?'准备语句中的参数

Use multiple '?' paremeter in prepared statement

我在尝试使用 C++ 中的 insert 语句时遇到一些 运行-time 错误。我想使用 2more "?" 来插入电子邮件和 用户名 玩家 但我遇到了一些错误。知道问题出在哪里吗?

string userInfo = "INSERT INTO PLAYERTABLE (EmailAddress,UserName)";
        userInfo += " VALUES (?,?)"; 

    PreparedStatement *prepareStatement;
    (*connection)->setAutoCommit(false); //Disable auto commit

    prepareStatement = (*connection)->prepareStatement(TABLEUSERFRIENDS);

    //Make sure User input is correct is NOT MySQL Injection
    prepareStatement->setString(1, "tt@yahoo.com");

    prepareStatement->setString(2, "myusername");

    prepareStatement->executeUpdate();

    (*connection)->commit(); //Save Data

我得到的错误是

发生错误是因为您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在 ' 附近使用的正确语法 PLAYERTABLE ' 在第 1 行

这是来自 prepare_statement.cpp (http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-prepared-statements.html) 的示例:

/*The first useful example - prepare() once, execute() n + 1 times 
 NOTE: The MySQL Server does not support named parameters. You have to use 
 the placeholder syntax shown below. There is no emulation which would you 
 allow to use named parameter like ':param1'. Use '?'. Parameters are 1-based. 
 */ 
 num_rows = 0; 
 prep_stmt.reset(con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)")); 
 for (i = 0; i < EXAMPLE_NUM_TEST_ROWS; i++) { 
    prep_stmt->setInt(1, test_data[i].id); 
    prep_stmt->setString(2, test_data[i].label); 
    /* executeUpdate() returns the number of affected = inserted rows */ 
    num_rows += prep_stmt->executeUpdate(); 
 } 

所以需要将SQL(userInfo)作为参数传递给prepareStatement函数