I am makinga code to send a query for my SQLlite database, but I just can't find a way to add a variable to the Char * variable that is being used as the query. the query and the variable that I want to add are declared like this:
String p = "10004F1F7";
char *sql = "SELECT * from TABELTAGGEGEVENS WHERE ID =" + p;
the error i get is this: error: invalid user-defined conversion from 'Arp::BasicString' to 'char*' [-fpermissive]
many thanks.
This doesn't work because in C++ the '+' operator on char pointers doesn't concatenate the strings. One solution would be to make the literal value a String as well:
String p = "10004F1F7";
String query = "SELECT * from TABELTAGGEGEVENS WHERE ID =";
You can then concatenate like this: +
operator: p + operator
I don't know the particular library you appear to be working with ( Arp::BasicString
), so I don't know how you'd convert that into a char *.
With std::string you can simply call c_str
on the result.
Another and probably better solution is to use formatters.
For reference see:
String p = "10004F1F7";
char *sql = "SELECT * from TABELTAGGEGEVENS WHERE ID =" + p;
You can handle the Arp::BasicString
almost like an std::string.
Looking through the headers you will find a Method named "CStr()"
.
Like that you should be able to do something like this:
String P = "10004F1F7";
String Query = "SELECT * from TABELTAGGEGEVENS WHERE ID =" + p;
char *sql = Query.CStr()
The only hit on Google for Arp::BasicString
was used in the SDK for a software company, PLCnext. A little bit of riffling through I found a header file BasicString.hxx
and inside a prototype for BasicString
class template. There the baseString
data structure is private.
I had to come up with this (rather low-level) workaround, compiling with PLCnext software succeeded and passed tests when adjusted for std::string
):
String p = "10004F1F7";
const char* CMD_SEQUENCE = "SELECT * from TABELTAGGEGEVENS WHERE ID =";
const int CMD_LENGTH = 41;
// allocate and assign memory for the static characters in the command
char *sql = (char *)malloc(CMD_LENGTH * sizeof(char));
memcpy(sql, CMD_SEQUENCE, CMD_LENGTH);
// iterate through all chars in String p
// resizing the memory buffer as needed and adding ith char to the end
for (int i=0; i<p.Size();i++){
sql = (char*)realloc(sql, (CMD_LENGTH + i) * sizeof(char));
// destination is the ith memory cell past the cmd sequence
int destIdx = CMD_LENGTH + i;
// copy 1 char at a time; ith char in p
memcpy( &sql[destIdx], &p.At(i), sizeof(char) );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.