c++ mysql update with variable

Jul 29, 2014 at 1:18pm
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...
Jul 29, 2014 at 1:28pm
1
2
std::string query = "UPDATE ControlloCorrente SET ValoreAttuale = " + value + " WHERE ID = '01'";
mysql_query(mysql1, query.c_str());
Last edited on Jul 29, 2014 at 1:45pm
Jul 29, 2014 at 1:36pm
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
Jul 29, 2014 at 1:38pm
Did you #include <string> ?
Jul 29, 2014 at 1:42pm
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
Jul 29, 2014 at 1:44pm
Oh whoops. See my edit.

Er nevermind. Don't need to escape those quotes.
Last edited on Jul 29, 2014 at 1:46pm
Jul 29, 2014 at 1:48pm
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
Jul 29, 2014 at 1:56pm
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.
Jul 29, 2014 at 2:12pm
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?
Jul 29, 2014 at 2:28pm
but how I can do to try?

Not sure what you mean there? Just change the type of value from int to std::string.
Jul 29, 2014 at 2:33pm
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());

Jul 29, 2014 at 2:43pm
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?
Jul 29, 2014 at 2:47pm
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 Jul 29, 2014 at 3:09pm
Jul 29, 2014 at 2:52pm
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
Jul 29, 2014 at 3:08pm
Christ. See my edit again. These are basic errors. You should not need to have your hand held through them.
Last edited on Jul 29, 2014 at 3:09pm
Jul 29, 2014 at 3:17pm
Ok, it work!!!!
Thank you very very much
Topic archived. No new replies allowed.