Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add " to a String
Forum Updated to NodeBB v4.3 + New Features

How to add " to a String

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JulianJ Offline
    JulianJ Offline
    Julian
    wrote on last edited by Julian
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #2

      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 !

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • JulianJ Offline
          JulianJ Offline
          Julian
          wrote on last edited by
          #4

          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

          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • JulianJ Julian

            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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @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"
            "\")");

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • JulianJ Julian

              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

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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 
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved