Mysql查询字符串在查询c++中不工作
Mysql query string not working in query c++
嗨,我有问题得到一个字符串变量到我的MySQL查询我已经尝试了一切,我不知道我在哪里出错任何建议,请。
string timestamp;
if (mysql_query(MySQLConnection, "INSERT INTO tablemeters (timestamp,gasreading,electricreading)VALUES ('"+timestamp+"', 'gas', 'elec')"))
获取错误是:
menucurl.cpp:169:142:错误:无法将参数' 2 '转换为' int mysql_query(MYSQL*, const char*) '的' std::basic_string '转换为' const char* '
您需要具有兼容的操作。+
对const char*
不起作用。
您应该更好地格式化您的查询,使用std::ostringstream
并将最终结果作为const char*
传递给mysql_query()
方法:
std::string timestamp;
std::ostringstream sqlStatement;
sqlStatement << "INSERT INTO
tablemeters (
timestamp ,
gasreading,
electricreading
)
VALUES
('" << timestamp << ',' <<
''' << gas << "'," <<
''' << elec << "')";
// Check the literal text of the SQL statement before passing it to mysql_query()
typedef std::cout logger;
logger << "Executing SQL command: "" << sqlStatement.str() """ << std::endl;
if (mysql_query(MySQLConnection, sqlStatement.str().c_str())) {
// ...
}
试试这样:
std::string timestamp;
std::string query = "INSERT INTO tablemeters (timestamp,gasreading,electricreading) VALUES ('"+timestamp+"', 'gas', 'elec')";
if (mysql_query(MySQLConnection, query.c_str()))...
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- QSqlquery prepare()和bindvalue()不工作
- 查询SQLite数据库中的日期
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 如何在ArangoDb AQL查询中指定数据库
- QSqlDatabasePrivate::removeDatabase:连接'myConnectionName'仍在使用中,所有查询将停止工作
- 使查询在 MSADO 的访问中工作
- C++程序中存在SQL查询语法错误,但可在MySQL工作台中使用
- C++ SQLite 查询只工作一次。我的代码有什么问题?
- 杀死一个Linux进程,停止它在Oracle数据库中的查询工作吗?
- Qt Sqlite查询不工作
- Mysql查询字符串在查询c++中不工作
- 针对 Boost ASIO 的设计,用于 Web 服务器的工作线程 SQl 查询'practical'
- sqlite更新查询不能在c++中工作