1

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.

5
  • 1
    What's String? Why is the "String p = inside the string as well? Commented Nov 26, 2019 at 9:21
  • 2
    Why do you think adding a literal string to an object of whatever String is would result in a char *? Commented Nov 26, 2019 at 9:22
  • what is Arp::BasicString ? Did you consult its documentation? Does it have a conversion to a c array of char? Commented Nov 26, 2019 at 9:22
  • How are you expecting the memory holding this string to be managed? Also, what's a String exactly? Is it coming from some library you're using? Commented Nov 26, 2019 at 9:44
  • 1
    Make sql an std::string, and add the two strings with std::string's operator+ If, for some reason, you need to pass sql as a char*, use std::string's c_str(). Note it returns a const char*, if you need a non-const pointer, check whether the function actually changes the string, in which case you'll have to copy it to a char array, otherwise you could const_cast (ugly as it is). Commented Nov 26, 2019 at 9:50

3 Answers 3

2

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:

Sign up to request clarification or add additional context in comments.

Comments

2
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() 

1 Comment

This is even better than the accepted answer, because it gives a simple explanation for how to convert the resulting string to char *
0

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) );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.