我试图在不使用c++中的mysql连接器类的情况下更新mysql数据库。但是我不知道如何将长整数添加到查询可以接受的字符串中。
#define CONVERSION 16
long myCounter = 0 ;
long gallons = 0;
gallons = myCounter / CONVERSION;
string sql = ("UPDATE utilityroom.Sprinklers SET "
"gallons='@gallons' WHERE label='PUMP';");
query_state = mysql_query(connection, sql);
result = mysql_store_result(connection);
它在查询中的第二个参数处一直失败。
错误:无法将‘std::string {aka std::basic_string}’转换为‘const char*’,将参数‘2’转换为‘int mysql_query(MYSQL*,const char*)’
编辑:
我讨厌c++处理字符串的方式。我不明白为什么每个字符串都要转换成类元素!
以下是我为使其正常工作所做的更改:
#include <sstream>
ostringstream strstr;
strstr << "UPDATE utilityroom.Sprinklers SET gallons='" << gallons << "' WHERE label='PUMP';";
string sql = strstr.str();
query_state = mysql_query(connection, sql.c_str());
发布于 2017-01-15 21:11:35
只需使用
query_state = mysql_query(connection, sql.c_str());
不存在从std::string到const char*的隐式转换。
https://stackoverflow.com/questions/41666357
复制相似问题