0

Hi I am having problems getting a string variable into my MySQL query I have tried everything I don't know where im going wrong any suggestions please.

string timestamp;     

if (mysql_query(MySQLConnection, "INSERT INTO tablemeters (timestamp,gasreading,electricreading)VALUES ('"+timestamp+"', 'gas', 'elec')"))

Error im getting is :

menucurl.cpp:169:142: error: cannot convert ‘std::basic_string’ to ‘const char*’ for argument ‘2’ to ‘int mysql_query(MYSQL*, const char*)’

n4zg
  • 387
  • 2
  • 6
  • 20

2 Answers2

3

You'll need to have compatible operations. + doesn't work for const char*.
You should better format your query, using a std::ostringstream and pass the final result as const char* to the mysql_query() method:

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())) {
    // ...
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks for the reply im getting the following errors menucurl.cpp:174:17: warning: missing terminating " character [enabled by default] menucurl.cpp:174:1: error: missing terminating " character menucurl.cpp:175:11: warning: character constant too long for its type [enabled by default] menucurl.cpp:175:42: warning: multi-character character constant [-Wmultichar] menucurl.cpp:175:49: warning: multi-character character constant [-Wmultichar] menucurl.cpp:175:56: warning: missing terminating " character [enabled by defaul – n4zg Jun 26 '14 at 19:49
  • @n4zg That looks related to something completely different from your code. Did my proposed code fix the error you got on that particular line, you mentioned in your sample? Please ask one question after an other, if the stuff isn't related. – πάντα ῥεῖ Jun 26 '14 at 19:52
  • hi it was the error I received when I added you original solution.. with the new solution I am getting 1 error -- menucurl.cpp:176:43: error: cannot convert ‘const char*’ to ‘MYSQL* {aka st_mysql*}’ for argument ‘1’ to ‘int mysql_query(MYSQL*, const char*)’ – n4zg Jun 26 '14 at 19:56
  • @n4zg Sorry, that was a typo more or less, you have had `MySQLConnection, ` already there in your sample, and I missed to replicate this. See the edited code ... And for your side: Improve your _c++ error message interpretation_ fu (comes with the years ;) ). – πάντα ῥεῖ Jun 26 '14 at 20:13
  • Hi that is compiling now but throwing this error once running Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '100','0.026')' at line 1 – n4zg Jun 26 '14 at 20:18
  • @n4zg _'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version'_ That's a pretty clear message, isn't it? Then go there, and modify the SQL syntax as needed. I'm pretty sure my formatting more or less replicates, what you wanted to provide originally. Put a `std::cout << sqlStatement.str() << std::endl;` statement before the `mysql_query()` call, and check if it's syntactically correct with some direct MySQL query tool. Note: I'm not going to _spoonfeed_ you here now! – πάντα ῥεῖ Jun 26 '14 at 20:25
  • @n4zg Also note: Since I didn't know, what the actual DB column types `gas` and `elec` are, could well be that you want to omit the surrounding `'` characters in the SQL statement. – πάντα ῥεῖ Jun 26 '14 at 20:32
  • it was a missing ' .. thank you very much – n4zg Jun 26 '14 at 21:08
1

Try something like this:

std::string timestamp;
std::string query = "INSERT INTO tablemeters (timestamp,gasreading,electricreading) VALUES ('"+timestamp+"', 'gas', 'elec')";

if (mysql_query(MySQLConnection, query.c_str()))...
tvlada
  • 21
  • 3