C++ Mysql查询字符串在查询c++;
嗨,我在MySQL查询中输入字符串变量时遇到问题,我已经尝试了所有方法,我不知道哪里出了问题,请给我一些建议C++ Mysql查询字符串在查询c++;,c++,mysql,C++,Mysql,嗨,我在MySQL查询中输入字符串变量时遇到问题,我已经尝试了所有方法,我不知道哪里出了问题,请给我一些建议 string timestamp; if (mysql_query(MySQLConnection, "INSERT INTO tablemeters (timestamp,gasreading,electricreading)VALUES ('"+timestamp+"', 'gas', 'elec')")) im获取的错误是: menucurl.cpp:169:142:
string timestamp;
if (mysql_query(MySQLConnection, "INSERT INTO tablemeters (timestamp,gasreading,electricreading)VALUES ('"+timestamp+"', 'gas', 'elec')"))
im获取的错误是:
menucurl.cpp:169:142:错误:无法将参数“2”的“std::basic_string”转换为“const char*”,将其转换为“int mysql_query(mysql*,const char*)”
您需要有兼容的操作<代码>+不适用于
常量字符*
您最好使用
std::ostringstream
格式化查询,并将最终结果作为const char*
传递给mysql\u 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::字符串时间戳;
std::ostringstreamsqlstatement;
sqlStatement您需要有兼容的操作<代码>+
不适用于常量字符*
您最好使用
std::ostringstream
格式化查询,并将最终结果作为const char*
传递给mysql\u 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::字符串时间戳;
std::ostringstreamsqlstatement;
sqlStatement尝试以下方法:
std::string timestamp;
std::string query = "INSERT INTO tablemeters (timestamp,gasreading,electricreading) VALUES ('"+timestamp+"', 'gas', 'elec')";
if (mysql_query(MySQLConnection, query.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()))...
感谢您的回复,我在编译错误1305后收到以下错误:FUNCTION timestamp.c_str不存在您需要在c_str
之后添加()
,以便它知道它是一个函数调用。您还需要对整个字符串执行此操作,不仅仅是在时间戳上。感谢您的回复,我在编译错误1305后收到以下错误:FUNCTION timestamp.c_str不存在您需要在c_str
之后添加()
,以便它知道它是一个函数调用。您还需要对整个字符串执行此操作,不仅仅是时间戳。感谢您的回复,我收到了以下错误menucurl。cpp:174:17:警告:缺少终止字符[默认启用]menucurl。cpp:174:1:错误:缺少终止字符menucurl。cpp:175:11:警告:字符常量对于其类型而言太长[默认启用]menucurl.cpp:175:42:警告:多字符字符常量[-Wmultichar]menucurl.cpp:175:49:警告:多字符常量[-Wmultichar]menucurl.cpp:175:56:警告:缺少终止字符[由启用]defaul@n4zg这看起来与您的代码完全不同。我建议的代码是否修复了您在示例中提到的特定行中遇到的错误?如果这些内容不相关,请一个接一个地提问。您好,这是我在向您添加原始解决方案时收到的错误。使用新解决方案,我我收到1个错误--menucurl.cpp:176:43:错误:无法将参数“1”的“const char*”转换为“MYSQL*{aka st_MYSQL*”,将其转换为“int MYSQL\u query(MYSQL*,const char*)“N4ZG抱歉,这或多或少是一个打印错误,你已经在你的样本中已经有了<代码> MySQL连接,,我错过了复制这个。请看编辑代码……并为你的一面:改进你的C++错误信息解释FU(随时间而来)。)。您好,现在正在编译,但运行错误1064后会抛出此错误:您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,在第1行“100”、“0.026”)附近使用正确的语法我收到以下错误menucurl.cpp:174:17:警告:缺少终止”字符[默认启用]菜单URL.cpp:174:1:错误:缺少终止“字符菜单URL.cpp:175:11:警告:字符常量对于其类型而言太长[默认启用]菜单URL.cpp:175:42:警告:多字符常量[-Wmultichar]菜单URL.cpp:175:49:警告:多字符常量[-Wmultichar]menucurl.cpp:175:56:警告:缺少“终止”字符[由启用]defaul@n4zg这看起来与您的代码完全不同。我建议的代码是否修复了您在示例中提到的特定行中遇到的错误?如果这些内容不相关,请一个接一个地提问。您好,这是我在向您添加原始解决方案时收到的错误。使用新解决方案,我我收到1个错误--menucurl.cpp:176:43:错误:无法将参数“1”的“const char*”转换为“MYSQL*{aka st_MYSQL*”,将其转换为“int MYSQL\u query(MYSQL*,const char*)“N4ZG抱歉,这或多或少是一个打印错误,你已经在你的样本中已经有了<代码> MySQL连接,,我错过了复制这个。请看编辑代码……并为你的一面:改进你的C++错误信息解释FU(随时间而来)。)。您好,现在正在编译,但运行错误1064后会抛出此错误:您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解第1行“100”、“0.026”)附近使用的正确语法