在准备好的语句中使用多个"?"参数表
Use multiple '?' paremeter in prepared statement
在c++中尝试使用insert语句时,我得到一些运行时错误。我想用 2 或更多 " ?"插入电子邮件和用户名到播放器,但我得到一些错误。知道问题出在哪里吗?
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 ' at line 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函数。
相关文章:
- 使用准备好的语句大容量插入到postgresql
- 当 std::future 准备好被检索时发出信号主线程
- 使用 std::async 准备好后立即显示结果
- 如何确定网络堆栈何时准备好再次打开到同一主机/端口的套接字?
- clang的libc++生产准备好了吗?
- 多卷曲C++ 准备好时不断提供新的处理程序
- 可以跨线程共享准备好的语句
- 将数值绑定到准备好的sql的正确方法
- C++套接字从未准备好使用轮询进行写入
- MysqlC++驱动程序-创建后获取准备好的语句查询
- 这是传递可选/NULL shared_ptr参数的好习惯吗?
- 如何在不使用准备好的OpenCv函数的情况下在c++中实现对图像的不锐利遮罩
- QSqlQuery:在执行之前获取准备好的语句字符串
- 我如何积累数据,直到它准备好通过串行发送
- 是写一个按值函数参数的好风格
- Qt Qcamera未准备好
- SQLite 查询未准备好行,但为空值
- 在我准备好分配它之前,将“unique_ptr”保留为未设置:这就是我重新分配它的方式吗?
- 通过libcouchbase-cxx发出准备好的/参数化的查询
- 在准备好的语句中使用多个"?"参数表