c++ mysql update with variable

Hi to all.
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <stdlib.h>
#include <stdio.h>
#include <mysql/mysql.h>
int value;

#define DATABASE_NAME "pippo"
#define DATABASE_USERNAME "pippo"
#define DATABASE_PASSWORD "pippo"
MYSQL *mysql1;

void mysql_connect (void)
{
    //initialize MYSQL object for connections
mysql1 = mysql_init(NULL);

    if(mysql1 == NULL)
    {
        fprintf(stderr, "ABB : %s\n", mysql_error(mysql1));
        return;
    }

    //Connect to the database
    if(mysql_real_connect(mysql1, "localhost", DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, 0, NULL, 0) == NULL)
    {
    fprintf(stderr, "%s\n", mysql_error(mysql1));
        return;
    }

    //Connect to the database
    if(mysql_real_connect(mysql1, "localhost", DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, 0, NULL, 0) == NULL)
    {
    fprintf(stderr, "%s\n", mysql_error(mysql1));
    }
    else
    {
//        printf("Database connection successful.\r\n");
    }
}

void mysql_disconnect (void)
{
    mysql_close(mysql1);
//    printf( "Disconnected from database.\r\n");
}

int main(int argc, char **argv) {

 mysql_connect();

 mysql_query(mysql1, "UPDATE ControlloCorrente SET ValoreAttuale = 100 WHERE ID = '01'");

 mysql_disconnect();

 exit(0);
}


The code is ok but I would change the fix value whith a variable like this:
1
2
int value = 100;
mysql_query(mysql1, "UPDATE ControlloCorrente SET ValoreAttuale = value WHERE ID = '01'")


Someone can help me?
And sorry for my English...
1
2
std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + value + " WHERE ID = '01'";
mysql_query(mysql1, query.c_str());
Last edited on
Thank ResidentBiscuit, I try to add your code but when I compile there are this error:

g++ -c -o RichiestaCorrente.o RichiestaCorrente.cpp
RichiestaCorrente.cpp: In function ‘int main(int, char**)’:
RichiestaCorrente.cpp:94:3: error: ‘string’ is not a member of ‘std’
RichiestaCorrente.cpp:94:15: error: expected ‘;’ before ‘query’
RichiestaCorrente.cpp:95:23: error: ‘query’ was not declared in this scope
make: *** [RichiestaCorrente.o] Error 1
Did you #include <string> ?
I add #include <string> now and the new error is

g++ -c -o RichiestaCorrente.o RichiestaCorrente.cpp
RichiestaCorrente.cpp: In function ‘int main(int, char**)’:
RichiestaCorrente.cpp:95:81: error: invalid operands of types ‘const char*’ and ‘const char [17]’ to binary ‘operator+’
make: *** [RichiestaCorrente.o] Error 1
Oh whoops. See my edit.

Er nevermind. Don't need to escape those quotes.
Last edited on
Also after your edit, the error is the same:

g++ -c -o RichiestaCorrente.o RichiestaCorrente.cpp
RichiestaCorrente.cpp: In function ‘int main(int, char**)’:
RichiestaCorrente.cpp:98:81: error: invalid operands of types ‘const char*’ and ‘const char [17]’ to binary ‘operator+’
make: *** [RichiestaCorrente.o] Error 1
Ah I see. The problem is value is an int, which is being cast to a const char.

If you happen to be using a C++11 compiler, this is fairly trivial.

1
2
std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + std::to_string(value) + " WHERE ID = '01'";
mysql_query(mysql1, query.c_str());


If you're not using a C++ compiler, you can use stringstreams to convert value to a string. See http://www.cplusplus.com/articles/D9j2Nwbp/

That's kind of a lot of code for such a trivial ordeal though (C++'s string facilities suck). If you're not actually using value as an int (ie, you're not performing any arithmetic on it), then just change it to a std::string.
Thank you for your patience...

I try with
1
2
3
 std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + std::to_string(value) + " WHERE ID = '01'";
 mysql_query(mysql1, query.c_str());


but I obtain this error

g++ -c -o RichiestaCorrente.o RichiestaCorrente.cpp
RichiestaCorrente.cpp: In function ‘int main(int, char**)’:
RichiestaCorrente.cpp:104:72: error: ‘to_string’ is not a member of ‘std’
make: *** [RichiestaCorrente.o] Error 1


I try this
1
2
3
4
  string String = static_cast<ostringstream*>( &(ostringstream() << value) )->str();

  std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + String + " WHERE ID = \'01\'";
  mysql_query(mysql1, query.c_str());


but I obtain this error
g++ -c -o RichiestaCorrente.o RichiestaCorrente.cpp
RichiestaCorrente.cpp: In function ‘int main(int, char**)’:
RichiestaCorrente.cpp:99:3: error: ‘string’ was not declared in this scope
RichiestaCorrente.cpp:99:3: note: suggested alternative:
/usr/include/c++/4.6/bits/stringfwd.h:65:33: note: ‘std::string’
RichiestaCorrente.cpp:99:10: error: expected ‘;’ before ‘String’
RichiestaCorrente.cpp:101:73: error: ‘String’ was not declared in this scope
make: *** [RichiestaCorrente.o] Error 1

I want to consider also your last quote:
That's kind of a lot of code for such a trivial ordeal though (C++'s string facilities suck). If you're not actually using value as an int (ie, you're not performing any arithmetic on it), then just change it to a std::string.

but how I can do to try?
but how I can do to try?

Not sure what you mean there? Just change the type of value from int to std::string.
As ResidentBiscuit suggested:
1
2
3
std::string value = "100";  // As a string
std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + value + " WHERE ID = '01'";
mysql_query(mysql1, query.c_str());

So this is ok, but I can't change the type of value. It must be an int.
It's difficult for me to explain in English... I read a sensor value whith this line

 
 value = mySwitch.getReceivedValue();


I try to declare value as a string but is not good
Have you other solution?
Then do:

1
2
3
4
5
6
7
8
#include <string>
#include <sstream>
...

std::string String = static_cast<std::ostringstream*>( &(std::ostringstream() << value) )->str();

std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + String + " WHERE ID = \'01\'";
mysql_query(mysql1, query.c_str());


You just forgot the std:: in front of string before.

Or update your compiler to a version which supports C++11. I mean, it's 2014. There's really no reason to be writing new code in anything but C++11.
Last edited on
I had already tried but obtain this error

g++ -c -o RichiestaCorrente.o RichiestaCorrente.cpp
RichiestaCorrente.cpp: In function ‘int main(int, char**)’:
RichiestaCorrente.cpp:115:36: error: expected type-specifier before ‘ostringstream’
RichiestaCorrente.cpp:115:36: error: expected ‘>’ before ‘ostringstream’
RichiestaCorrente.cpp:115:36: error: expected ‘(’ before ‘ostringstream’
RichiestaCorrente.cpp:115:36: error: ‘ostringstream’ was not declared in this scope
RichiestaCorrente.cpp:115:36: note: suggested alternative:
/usr/include/c++/4.6/iosfwd:150:38: note: ‘std::ostringstream’
RichiestaCorrente.cpp:115:50: error: expected primary-expression before ‘>’ token
RichiestaCorrente.cpp:115:89: error: expected ‘)’ before ‘;’ token
make: *** [RichiestaCorrente.o] Error 1
Christ. See my edit again. These are basic errors. You should not need to have your hand held through them.
Last edited on
Ok, it work!!!!
Thank you very very much
Topic archived. No new replies allowed.