How to add " to a String



  • Hello, I need to add "" to a String and it do not considers. For example:

     QString list;
      list.append("CREATE TABLE IF NOT EXISTS ");
       list.push_back(ui->lineEdit_interseccion->text());  //this is the tabla name
    
     list.push_back("("
                        "a TEXT primary key,"
                        "j TEXT"
                        ");");
        ui->label->setText(list);
    

    And with that seTex I get:

    CREATE TABLE IF NOT EXISTS Soler y Sarabia (a TEXT primary key, j TEXT)

    So as you can see there is not "" between ().

    How can I put it?



  • Hi @Julian

    let's say you have :

    QString tabla = "yourString";
    

    if you want to add " to your string you can do someting like this :

    tabla.push_back("\"");
    

    The result will be then :

    yourString"
    

    The key is to have \" instead of " , \ allow you actullay to escape the character "

    Hope this can help !



  • You can either escape (i.e. add a \ before) the " or use raw string literals

    • "I have a \" in the Middle"
    • R"**(I have a " in the Middle)**"

    P.S.

    list.append("CREATE TABLE IF NOT EXISTS ");
       list.push_back(ui->lineEdit_interseccion->text());  //this is the tabla name
    

    This is a MAJOR liability to your database, see https://www.w3schools.com/sql/sql_injection.asp



  • Thanks! I have a problem here

     QString consulta;
      consulta.append("CREATE TABLE IF NOT EXISTS ");
        consulta.push_back(ui->lineEdit_interseccion->text()  ); //this it's the table name. 
    
    

    then I do

    
        consulta.push_back(" ( \" a TEXT primary key, \"  \"j TEXT\" \");");
    

    and there is the problem.

    alt text


  • Moderators

    @Julian You should really read about escaping characters in C/C++.
    You're escaping first " in second and third line ("j TEXT"...).
    You should not because it indicates the beginning of a string!
    It should be:

    "\"j TEXT"
    "\")");


  • @Julian

    The problem now is that you span your string over multiple rows in QtCreator. The new Chars in the new line need their own " to mark them as part of the previous string.

    //Correct MultiLineString
    QString s = "Line1 "
                          "\"Line2\" "
                          "Line3 "
    //Results in
    //s =  Line1 "Line2" Line3 
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.