Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to format SQL strings?

How to format SQL strings?

Scheduled Pinned Locked Moved Qt Creator and other tools
8 Posts 4 Posters 2.9k 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.
  • M Offline
    M Offline
    mbnoimi
    wrote on last edited by
    #1

    Hello,

    Does any one knows a tool, utility or Qt Creator plugin for formatting C++ friendly strings?

    This issue nagging me very much because whenever I copy SQL string I've to format the content manually before using it inside my C++\Qt source code... Example:

    I've to format:
    [code]"SELECT public."Accounts".name, public."Analyze".name, year_2014."Results".value, year_2014."Results"."date", year_2014."Results".requestnumber FROM year_2014."Results" INNER JOIN public."Accounts" ON (year_2014."Results".account_id = public."Accounts".id) INNER JOIN public."Analyze" ON (year_2014."Results".analyze_id = public."Analyze".id) WHERE public."Analyze".isparent = 'FALSE' AND public."Accounts".id = %1 OR public."Accounts".name LIKE '%%2%' UNION SELECT public."Accounts".name, public."SubAnalyze".name, year_2014."Results".value, year_2014."Results"."date", year_2014."Results".requestnumber FROM year_2014."Results" INNER JOIN public."Accounts" ON (year_2014."Results".account_id = public."Accounts".id) INNER JOIN public."SubAnalyze" ON (year_2014."Results".subanalyze_id = public."SubAnalyze".id) WHERE public."Accounts".id = %1 OR public."Accounts".name LIKE '%%2%'";[/code]

    To be like this:
    [code]"SELECT public."Accounts".name, public."Analyze".name, year_2014."Results".value, year_2014."Results"."date", year_2014."Results".requestnumber FROM year_2014."Results" INNER JOIN public."Accounts" ON (year_2014."Results".account_id = public."Accounts".id) INNER JOIN public."Analyze" ON (year_2014."Results".analyze_id = public."Analyze".id) WHERE public."Analyze".isparent = 'FALSE' AND public."Accounts".id = %1 OR public."Accounts".name LIKE '%%2%' UNION SELECT public."Accounts".name, public."SubAnalyze".name, year_2014."Results".value, year_2014."Results"."date", year_2014."Results".requestnumber FROM year_2014."Results" INNER JOIN public."Accounts" ON (year_2014."Results".account_id = public."Accounts".id) INNER JOIN public."SubAnalyze" ON (year_2014."Results".subanalyze_id = public."SubAnalyze".id) WHERE public."Accounts".id = %1 OR public."Accounts".name LIKE '%%2%'";[/code]

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      You could do a find+replace before pasting the string into your .cpp file. (e.g. in Microsoft Notepad, the shortcut is Ctrl+H)

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbnoimi
        wrote on last edited by
        #3

        Of course I already know that;

        My question is very specified. I need a tool for reformat this kind of strings.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          There is no such specific tool but you have two options:

          1. QtCreator supports some subset of Vim and its regular expressions.
            You can switch to Vim mode by Alt+V,Alt+V
            Put cursor on the string and enter to command mode by :

          For this particular string you can use the following RegEx and press enter.
          @
          s/."([^"]*)"/.\"\1\"/g
          @

          1. QtCreator "Search & Replace" also supports "RegEx":http://qt-project.org/doc/qtcreator-3.1/creator-editor-finding.html
            It is based on "QRegExp":http://qt-project.org/doc/qt-5/qregexp.html#details

          An expression to search
          @
          ."([^"]*)"
          @

          An expression to replace
          @
          ."\1"
          @

          [EDIT]; fixed a replace expression.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbnoimi
            wrote on last edited by
            #5

            [quote author="andreyc" date="1405395356"]There is no such specific tool
            [/quote]

            No there is a such tool; I used it before but it's a commercial one (Navicat: it has "Copy to C++" action which format the string for me); Beside that I remember that some contributor started a Qt Creator plugin adds "Special Paste" action for formatting such kind of strings but I missed his post (I read it in Qt's bug tracker).

            I'm sure there is such kind of tools (commercial and open source too) but I forgot it the correct term for find them in google.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              [quote]No there is a such tool; I used it before [/quote]
              Right. I guess I wanted to say that I don't know such tool.

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                I don't know of any such tool either, sorry.

                Good luck with your search.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mikeosoft
                  wrote on last edited by
                  #8

                  I might be wrong with this, I wouldn't consider myself an SQL expert, but this seems to works for me using SQLite within Qt.

                  In my SQL I use the single quote ' rather than the double quote " and it doesn't need escaping, so I avoid the problem.

                  I would also have thought that table and field names only need quotation marks if they have spaces in them. I use underscores in field names to avoid that and only use the single quote when I am including strings for things like date formats within the strftime function.

                  I guess things could be different with different SQL libraries within Qt?

                  1 Reply Last reply
                  0

                  • Login

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