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. Trying to PARSE text in a SQL query between SELECT and FROM
Forum Updated to NodeBB v4.3 + New Features

Trying to PARSE text in a SQL query between SELECT and FROM

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 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.
  • P Offline
    P Offline
    petschephp
    wrote on 18 Jun 2013, 09:12 last edited by
    #1

    All I want to do is select the text in between SELECT and FROM in the SQL query below.
    I did a little reading on QRegExp but have still been unsuccessful at finding a solution.
    @
    SELECT

    • users.id,
      users.username AS username,
      users.email AS email,
      users.first_name AS firstName,
      users.last_name AS lastName,
      users_groups.user_id,
      groups.name AS groupName*
      FROM
      users
      INNER JOIN
      users_groups
      ON
      (
      users.id =users_groups.user_id)
      INNER JOIN
      groups
      ON
      (
      users_groups.group_id =groups.id) ;
      @
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Jun 2013, 09:18 last edited by
      #2

      Hi and welcome to devnet,

      You can use a combination of QString's "indexOf":http://qt-project.org/doc/qt-4.8/qstring.html#indexOf combined with "mid":http://qt-project.org/doc/qt-4.8/qstring.html#mid to achieve this

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 18 Jun 2013, 09:19 last edited by
        #3

        Sounds easy? Just look for the location of SELECT and of FROM in the string, and use QString::mid to get the part of the string you're interested in. Works as long as you don't have complicated queries with subqueries. If you have that, you're going to need real lexing and parsing anyway, and regexps won't help you either.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          petschephp
          wrote on 18 Jun 2013, 10:21 last edited by
          #4

          That worked perfectly. The last thing I am trying to do is get the name of each column.

          Remaining SQL to get Columns From
          users.id AS id,
          users.username AS username,
          users.email AS email,
          users.first_name AS firstName,
          users.last_name AS lastName,
          users_groups.user_id AS user_id,
          groups.name AS groupName

          So I'm searching in between AS and ,
          qDebug() << betweenSelectAndFrom.indexOf("AS");
          qDebug() << betweenSelectAndFrom.indexOf(",");

          This code works great with QString::mid, however it only gets the first occurrence. How do I get all occurrences?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 18 Jun 2013, 10:24 last edited by
            #5

            I'd first split the fields using QString::split(), using the comma as the separator. Then for each string of the result of that operation, look for the AS clause.

            Might I ask what your real purpose is? So far, it seems to me that there is an easier way do get the field names that result from your query...

            1 Reply Last reply
            0
            • P Offline
              P Offline
              petschephp
              wrote on 18 Jun 2013, 11:06 last edited by
              #6

              I'm still fairly new to Qt so there may be a better solution.

              What I'm creating is a PHP code (CRUD) Generator. The specific purpose of this function is to generate the variables used in a PHP class. Sometimes my JOIN statement can contain 5+ different tables and I don't need every column in each table I only want the columns that are in the query itself.

              This is a simple example of the php code I'm trying to generate of what I'm trying to generate
              @
              class UserModel extends CActiveModel
              {

              • private $userId;
                
                private $username;*
                

              }
              @

              edit: please use @ tags around code sections; Andre

              1 Reply Last reply
              0

              6/6

              18 Jun 2013, 11:06

              • Login

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