1

i have defined a value using

#define username "admin"

then i want to use the username in a char statement into sql as

const char *pSQL[1];
pSQL[1] = "update websrv_config set admin_id='" + username + "'";

but it seems that there is an error

error: invalid operands of types ‘const char [36]’ and ‘const char [6]’ to binary ‘operator+’

how can i overcome it?

4

2 に答える 2

3

Try this:

pSQL[0] = "update websrv_config set admin_id='"  username  "'";

In c++ array index starts with 0 so when you have decleared an array of length 1. The only index it can have is 0. Further, '+' is not used for concatenation in c++.

于 2012-08-27T21:16:51.117 に答える
2

That's a C-string, not a std::string (and what you'd do in C, not C++), but if you must, you can do this:

pSQL[0] = "update websrv_config set admin_id='"  username  "'";

C-strings can't be concatenated with + because they're string literals.

I'd use a std::string if I were you:

std::string sql = "update websrv_config set admin_id='";
sql += username;
sql += "'";

and you can get the contents in const char* format (if needed) with

sql.c_str();

Also, note that since username is a macro, you can't change it at runtime.

Also, if you get rid of the macro, think about some evil mastermind providing a username like:

"johnny'; DROP TABLE websrv_config;"
于 2012-08-27T21:13:03.570 に答える