-3

i have goal to send data from C++ code to mariaDB. I'm struggling with compilation. Will be happy as an elephant if you can help me how to send float into database. :)

float pi=3.14; 
     if (mysql_query(conn, "INSERT INTO meranie VALUES ('%f')",pi) != 0)
  {
    fprintf(stderr, "Query Failure\n");
    return EXIT_FAILURE;
  }
SimonLi
  • 13
  • 4

1 Answers1

2

You can try (C++):

float pi=3.14;
std::string insert = "INSERT INTO meranie VALUES ("+std::to_string(pi)+")";
if (mysql_query(conn, insert.c_str()) != 0){
    fprintf(stderr, "Query Failure\n");
    return EXIT_FAILURE;
}

If you want to use only C, you can try sprintf.

Patrick
  • 21
  • 2
  • Welcome to StackOverflow! Please don't assume that everyone is `using namespace std;`, as that is [considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) by many people. Personally, I find your code hard to read, because without the `std::` prefix, I must always ask myself whether you are referring to a function of the MySQL API or the `std` namespace. You may want to edit your answer and add the `std::` prefix or at least add the line `using namespace std;` to make it obvious what is going on. I have upvoted your answer anyway. – Andreas Wenzel Feb 11 '20 at 01:09
  • Thanks. I didn't use __std::__ for speed and I forgot to mention __using namespace std;__. I'm a beginner in C ++. My main specialization is Java, so thank you very much for the correction. – Patrick Feb 11 '20 at 23:53