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. MySQL problem with inserting quote in my QSqlQuery prepare statement

MySQL problem with inserting quote in my QSqlQuery prepare statement

Scheduled Pinned Locked Moved Solved General and Desktop
qsqlqueryqstringquotes
20 Posts 4 Posters 1.6k Views
  • 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.
  • S Offline
    S Offline
    swankster
    wrote on 12 Feb 2024, 14:14 last edited by
    #1

    I have been struggling with a QSqlQuery prepare statement to a MySQL database. I am attempting put a set a phrase as a variable. ie phrase_var for "this is my phrase" I need to do a WHERE MATCH of said phrase.

    when I run the query within workbench it works perfectly. ```
    SELECT * FROM schema.table WHERE MATCH(phrase) AGAINST( '"this is my phrase"' IN BOOLEAN MODE);

    
    

    "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '"" + phrase_var + ""' IN BOOLEAN MODE)"

    "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( " +R"('")" + phrase_var + R"("')" + " IN BOOLEAN MODE)" );

    
    When viewing the prepared statement with qDebug()<<qry.executedQuery();  always displays as.  with \" instead of "
    

    SELECT * FROM schema.table WHERE MATCH(text) AGAINST( '"this is my phrase"' IN BOOLEAN MODE)"

    thankyou in advance for any assistant.
    J J 2 Replies Last reply 12 Feb 2024, 14:23
    0
    • S swankster
      12 Feb 2024, 14:14

      I have been struggling with a QSqlQuery prepare statement to a MySQL database. I am attempting put a set a phrase as a variable. ie phrase_var for "this is my phrase" I need to do a WHERE MATCH of said phrase.

      when I run the query within workbench it works perfectly. ```
      SELECT * FROM schema.table WHERE MATCH(phrase) AGAINST( '"this is my phrase"' IN BOOLEAN MODE);

      
      

      "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '"" + phrase_var + ""' IN BOOLEAN MODE)"

      "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( " +R"('")" + phrase_var + R"("')" + " IN BOOLEAN MODE)" );

      
      When viewing the prepared statement with qDebug()<<qry.executedQuery();  always displays as.  with \" instead of "
      

      SELECT * FROM schema.table WHERE MATCH(text) AGAINST( '"this is my phrase"' IN BOOLEAN MODE)"

      thankyou in advance for any assistant.
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 12 Feb 2024, 14:23 last edited by
      #2

      @swankster It is better to use https://doc.qt.io/qt-6/qsqlquery.html#prepare instead of assembling the query string manually.

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Ronel_qtmaster
        wrote on 12 Feb 2024, 14:41 last edited by
        #3

        @swankster an exemple
        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
        "VALUES (:id, :forename, :surname)");
        query.bindValue(":id", 1001);
        query.bindValue(":forename", "Bart");
        query.bindValue(":surname", "Simpson");
        query.exec();

        J 1 Reply Last reply 12 Feb 2024, 14:42
        0
        • S swankster
          12 Feb 2024, 14:14

          I have been struggling with a QSqlQuery prepare statement to a MySQL database. I am attempting put a set a phrase as a variable. ie phrase_var for "this is my phrase" I need to do a WHERE MATCH of said phrase.

          when I run the query within workbench it works perfectly. ```
          SELECT * FROM schema.table WHERE MATCH(phrase) AGAINST( '"this is my phrase"' IN BOOLEAN MODE);

          
          

          "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '"" + phrase_var + ""' IN BOOLEAN MODE)"

          "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( " +R"('")" + phrase_var + R"("')" + " IN BOOLEAN MODE)" );

          
          When viewing the prepared statement with qDebug()<<qry.executedQuery();  always displays as.  with \" instead of "
          

          SELECT * FROM schema.table WHERE MATCH(text) AGAINST( '"this is my phrase"' IN BOOLEAN MODE)"

          thankyou in advance for any assistant.
          J Offline
          J Offline
          JonB
          wrote on 12 Feb 2024, 14:41 last edited by
          #4

          @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

          When viewing the prepared statement with qDebug()<<qry.executedQuery(); always displays as. with " instead of "

          And that is how literal " characters in a string, so what is the issue?
          I'm not going to try to understand whether what you have is correct as you have posted it with the literal ``` stuff all overt the place, but why do you think you have any quoting issue?

          Whether MySQL accepts this or there is some other syntax error I cannot say.

          1 Reply Last reply
          0
          • R Ronel_qtmaster
            12 Feb 2024, 14:41

            @swankster an exemple
            QSqlQuery query;
            query.prepare("INSERT INTO person (id, forename, surname) "
            "VALUES (:id, :forename, :surname)");
            query.bindValue(":id", 1001);
            query.bindValue(":forename", "Bart");
            query.bindValue(":surname", "Simpson");
            query.exec();

            J Offline
            J Offline
            JonB
            wrote on 12 Feb 2024, 14:42 last edited by JonB 2 Dec 2024, 14:45
            #5

            @Ronel_qtmaster
            Do you have any evidence that bindValue() will be accepted by the MySQL driver for this MATCH ... AGAINST clause? Unless you have tried it you would not know (and I do not, but it would not surprise me if not). For example, LIKE and IN do not accept binds (as far as I recall).

            S R 2 Replies Last reply 12 Feb 2024, 14:48
            0
            • J JonB
              12 Feb 2024, 14:42

              @Ronel_qtmaster
              Do you have any evidence that bindValue() will be accepted by the MySQL driver for this MATCH ... AGAINST clause? Unless you have tried it you would not know (and I do not, but it would not surprise me if not). For example, LIKE and IN do not accept binds (as far as I recall).

              S Offline
              S Offline
              swankster
              wrote on 12 Feb 2024, 14:48 last edited by
              #6

              @JonB f
              yes, i have used bind as well with the exact same results.

              R J 2 Replies Last reply 12 Feb 2024, 14:49
              0
              • J JonB
                12 Feb 2024, 14:42

                @Ronel_qtmaster
                Do you have any evidence that bindValue() will be accepted by the MySQL driver for this MATCH ... AGAINST clause? Unless you have tried it you would not know (and I do not, but it would not surprise me if not). For example, LIKE and IN do not accept binds (as far as I recall).

                R Offline
                R Offline
                Ronel_qtmaster
                wrote on 12 Feb 2024, 14:48 last edited by
                #7

                @JonB I think his problem here is not the plugin but the syntax.And SQL is a general language so why would MYSQL plugin not accept this syntax?

                1 Reply Last reply
                0
                • S swankster
                  12 Feb 2024, 14:48

                  @JonB f
                  yes, i have used bind as well with the exact same results.

                  R Offline
                  R Offline
                  Ronel_qtmaster
                  wrote on 12 Feb 2024, 14:49 last edited by
                  #8

                  @swankster please post the code where you used bind, as well as the errors related to it

                  S 1 Reply Last reply 12 Feb 2024, 14:58
                  0
                  • S swankster
                    12 Feb 2024, 14:48

                    @JonB f
                    yes, i have used bind as well with the exact same results.

                    J Offline
                    J Offline
                    JonB
                    wrote on 12 Feb 2024, 14:54 last edited by JonB 2 Dec 2024, 14:55
                    #9

                    @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                    yes, i have used bind as well with the exact same results.

                    You have never told us of any "results". Don't know what your actual issue is. Maybe someone else does, but I cannot tell from what you have written. You could show error messages/results but you do not.

                    And SQL is a general language so why would MYSQL plugin not accept this syntax?

                    Because I have used MySQL and other SQLs for a long time and have never come across

                    MATCH(phrase) AGAINST( '"this is my phrase"' IN BOOLEAN MODE);

                    If you think this statement is somehow in "general SQL" it is not. Each SQL has its own various additions to "standard" SQL (whatever that is), and this would have to fall into that category. I don't know if MySQL has such a clause, and if what OP has written is syntactically correct. That is all I said.

                    S 1 Reply Last reply 12 Feb 2024, 15:09
                    0
                    • R Ronel_qtmaster
                      12 Feb 2024, 14:49

                      @swankster please post the code where you used bind, as well as the errors related to it

                      S Offline
                      S Offline
                      swankster
                      wrote on 12 Feb 2024, 14:58 last edited by
                      #10

                      @Ronel_qtmaster
                      "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );
                      qry.bindValue( ":phrase", phrase_var );

                      executedQuery() returns
                      SELECT * FROM language.master WHERE MATCH(phrase) AGAINST( '":phrase"' IN BOOLEAN MODE)"

                      what i need to see in my executequery is
                      SELECT * FROM schema.table WHERE MATCH(text) AGAINST( '"this is my phrase"' IN BOOLEAN MODE)"
                      how can I display '"this is my phrase"' without " appearing?

                      J R 2 Replies Last reply 12 Feb 2024, 15:07
                      0
                      • S swankster
                        12 Feb 2024, 14:58

                        @Ronel_qtmaster
                        "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );
                        qry.bindValue( ":phrase", phrase_var );

                        executedQuery() returns
                        SELECT * FROM language.master WHERE MATCH(phrase) AGAINST( '":phrase"' IN BOOLEAN MODE)"

                        what i need to see in my executequery is
                        SELECT * FROM schema.table WHERE MATCH(text) AGAINST( '"this is my phrase"' IN BOOLEAN MODE)"
                        how can I display '"this is my phrase"' without " appearing?

                        J Offline
                        J Offline
                        JonB
                        wrote on 12 Feb 2024, 15:07 last edited by
                        #11

                        @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                        "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );

                        If you used the forum's Code (</> button) wehere you are asking about quote characters we could see you are using

                        "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );
                        

                        So instead of '":phrase"' have you tried ':phrase', ":phrase" or :phrase?

                        S 1 Reply Last reply 12 Feb 2024, 15:26
                        0
                        • J JonB
                          12 Feb 2024, 14:54

                          @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                          yes, i have used bind as well with the exact same results.

                          You have never told us of any "results". Don't know what your actual issue is. Maybe someone else does, but I cannot tell from what you have written. You could show error messages/results but you do not.

                          And SQL is a general language so why would MYSQL plugin not accept this syntax?

                          Because I have used MySQL and other SQLs for a long time and have never come across

                          MATCH(phrase) AGAINST( '"this is my phrase"' IN BOOLEAN MODE);

                          If you think this statement is somehow in "general SQL" it is not. Each SQL has its own various additions to "standard" SQL (whatever that is), and this would have to fall into that category. I don't know if MySQL has such a clause, and if what OP has written is syntactically correct. That is all I said.

                          S Offline
                          S Offline
                          swankster
                          wrote on 12 Feb 2024, 15:09 last edited by
                          #12

                          @JonB
                          syntax works perfectly while in workbench. as stated initial post.

                          the output string is also stated from executedQuery where i am receiving a '"\phrase"' instead of '"phrase"'

                          J 1 Reply Last reply 12 Feb 2024, 15:12
                          0
                          • S swankster
                            12 Feb 2024, 15:09

                            @JonB
                            syntax works perfectly while in workbench. as stated initial post.

                            the output string is also stated from executedQuery where i am receiving a '"\phrase"' instead of '"phrase"'

                            J Offline
                            J Offline
                            JonB
                            wrote on 12 Feb 2024, 15:12 last edited by JonB 2 Dec 2024, 15:23
                            #13

                            @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                            syntax works perfectly while in workbench. as stated initial post.

                            But from workbench you are not using bound variables, are you? Or are you?
                            And your first post does not use bound variables, does it?
                            And you still have not said or shown what error you get where in SQL code?
                            Trying to help, but not seeming to get anywhere.

                            the output string is also stated from executedQuery where i am receiving a '"\phrase"' instead of '"phrase"'

                            Copy and paste error messages please. The characters here are vital and I do not think as you have written. And please post inside Code tags, you are asking question about punctuation characters which we cannot even read right!

                            AGAINST( '":phrase"' IN BOOLEAN MODE)"

                            This does not look right.

                            the output string is also stated from executedQuery where i am receiving a '"\phrase"' instead of '"phrase"'

                            Already said: qDebug() or debugger may show literal characters like ' or " inside strings with backslashes.

                            1 Reply Last reply
                            0
                            • S swankster
                              12 Feb 2024, 14:58

                              @Ronel_qtmaster
                              "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );
                              qry.bindValue( ":phrase", phrase_var );

                              executedQuery() returns
                              SELECT * FROM language.master WHERE MATCH(phrase) AGAINST( '":phrase"' IN BOOLEAN MODE)"

                              what i need to see in my executequery is
                              SELECT * FROM schema.table WHERE MATCH(text) AGAINST( '"this is my phrase"' IN BOOLEAN MODE)"
                              how can I display '"this is my phrase"' without " appearing?

                              R Offline
                              R Offline
                              Ronel_qtmaster
                              wrote on 12 Feb 2024, 15:25 last edited by
                              #14

                              @swankster Why do you want to display it without appearing first? Qt has its syntax

                              1 Reply Last reply
                              0
                              • J JonB
                                12 Feb 2024, 15:07

                                @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                                "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );

                                If you used the forum's Code (</> button) wehere you are asking about quote characters we could see you are using

                                "SELECT * FROM schema.table WHERE MATCH( phrase ) AGAINST( '":phrase"' IN BOOLEAN MODE)" );
                                

                                So instead of '":phrase"' have you tried ':phrase', ":phrase" or :phrase?

                                S Offline
                                S Offline
                                swankster
                                wrote on 12 Feb 2024, 15:26 last edited by
                                #15

                                @JonB
                                yes, i have tried ':phrase', ":phrase" and :phrase
                                ':phrase' returns ':phrase'
                                :phrase returns ?
                                it acts as though binding does not work with AGAINST()

                                in workbench, correct I am not using bound value. that is what im attempting to send to MySQL. it is somewhat working in that it will match any of the words within the phrase to the field. But I need this to check for exact phrase. if it exists to prompt the user that the particular phase is already set in the DB. if it does not exist it automatically tries to add. but the field is unique so it does error when attempting to create it as new if it already exists.

                                The WHERE MATCH AGAINST() does works in workbench with the exact phrase.

                                J R 2 Replies Last reply 12 Feb 2024, 15:32
                                0
                                • S swankster
                                  12 Feb 2024, 15:26

                                  @JonB
                                  yes, i have tried ':phrase', ":phrase" and :phrase
                                  ':phrase' returns ':phrase'
                                  :phrase returns ?
                                  it acts as though binding does not work with AGAINST()

                                  in workbench, correct I am not using bound value. that is what im attempting to send to MySQL. it is somewhat working in that it will match any of the words within the phrase to the field. But I need this to check for exact phrase. if it exists to prompt the user that the particular phase is already set in the DB. if it does not exist it automatically tries to add. but the field is unique so it does error when attempting to create it as new if it already exists.

                                  The WHERE MATCH AGAINST() does works in workbench with the exact phrase.

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 12 Feb 2024, 15:32 last edited by JonB 2 Dec 2024, 15:35
                                  #16

                                  @swankster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                                  it acts as though binding does not work with AGAINST()

                                  Which is what I suggested might well be the case right from the start.......

                                  @JonB said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                                  @Ronel_qtmaster
                                  Do you have any evidence that bindValue() will be accepted by the MySQL driver for this MATCH ... AGAINST clause? Unless you have tried it you would not know (and I do not, but it would not surprise me if not). For example, LIKE and IN do not accept binds (as far as I recall).

                                  I did not think he should suggest that to you unless he has tested it actually worked. See the earlier discussion. So unless you know the MySQL driver accepts binding for this clause do not use binding!

                                  Binding is accepted, or not accepted, by the driver on a case by case basis. It is not just a "macro substitution".

                                  1 Reply Last reply
                                  0
                                  • S swankster
                                    12 Feb 2024, 15:26

                                    @JonB
                                    yes, i have tried ':phrase', ":phrase" and :phrase
                                    ':phrase' returns ':phrase'
                                    :phrase returns ?
                                    it acts as though binding does not work with AGAINST()

                                    in workbench, correct I am not using bound value. that is what im attempting to send to MySQL. it is somewhat working in that it will match any of the words within the phrase to the field. But I need this to check for exact phrase. if it exists to prompt the user that the particular phase is already set in the DB. if it does not exist it automatically tries to add. but the field is unique so it does error when attempting to create it as new if it already exists.

                                    The WHERE MATCH AGAINST() does works in workbench with the exact phrase.

                                    R Offline
                                    R Offline
                                    Ronel_qtmaster
                                    wrote on 12 Feb 2024, 15:39 last edited by
                                    #17

                                    @swankster I would suggest you to try a simple binding syntax with Mysql and see which result you receive.So a statement different that what you want to achieve.If you have a positive result, then binding is working and your syntax is wrong.If not..

                                    J 1 Reply Last reply 12 Feb 2024, 17:39
                                    0
                                    • R Ronel_qtmaster
                                      12 Feb 2024, 15:39

                                      @swankster I would suggest you to try a simple binding syntax with Mysql and see which result you receive.So a statement different that what you want to achieve.If you have a positive result, then binding is working and your syntax is wrong.If not..

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 12 Feb 2024, 17:39 last edited by
                                      #18

                                      @Ronel_qtmaster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                                      and your syntax is wrong.If not..

                                      Sorry, but this is not necessarily correct, and you are repeating (what I believe to be) the same mistake as before, in advising the OP to use binding when you have no evidence that this SQL clause, even if correct, accepts parameter binding. You seem to think that anything which is syntactically correct can have bindings put into it, and that is not the case. Please read my earlier posts.

                                      @swankster
                                      Good luck if you can get binding to work for your MATCH ... AGAINST, I suspect you will not. Assuming that is the case, you just need to generate the correct literal sting which you have tested in workshop into C++ code. Qt's QString::arg() is a convenient way to generate QStrings which require C++ variables' values (your phrase_var) inserted into them.

                                      S 1 Reply Last reply 12 Feb 2024, 18:56
                                      0
                                      • J JonB
                                        12 Feb 2024, 17:39

                                        @Ronel_qtmaster said in MySQL problem with inserting quote in my QSqlQuery prepare statement:

                                        and your syntax is wrong.If not..

                                        Sorry, but this is not necessarily correct, and you are repeating (what I believe to be) the same mistake as before, in advising the OP to use binding when you have no evidence that this SQL clause, even if correct, accepts parameter binding. You seem to think that anything which is syntactically correct can have bindings put into it, and that is not the case. Please read my earlier posts.

                                        @swankster
                                        Good luck if you can get binding to work for your MATCH ... AGAINST, I suspect you will not. Assuming that is the case, you just need to generate the correct literal sting which you have tested in workshop into C++ code. Qt's QString::arg() is a convenient way to generate QStrings which require C++ variables' values (your phrase_var) inserted into them.

                                        S Offline
                                        S Offline
                                        swankster
                                        wrote on 12 Feb 2024, 18:56 last edited by
                                        #19

                                        @JonB
                                        binding does not work with Against. looks like I will need to do a different approach. Thanks

                                        J 1 Reply Last reply 12 Feb 2024, 18:59
                                        0
                                        • S swankster
                                          12 Feb 2024, 18:56

                                          @JonB
                                          binding does not work with Against. looks like I will need to do a different approach. Thanks

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 12 Feb 2024, 18:59 last edited by
                                          #20

                                          @swankster
                                          :) Like I said, just build the (correct, including whatever quoting, and if you are OK on possible SQL injection attacks!) string and pass that without using binding, for this query.

                                          1 Reply Last reply
                                          0
                                          • S swankster has marked this topic as solved on 12 Feb 2024, 19:00

                                          1/20

                                          12 Feb 2024, 14:14

                                          • Login

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