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. [SOLVED] Help with QSqlQueryModel
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Help with QSqlQueryModel

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 6.0k 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.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #3

    Thanks for your reply mcosta.

         Even if I don't use the *GROUP BY* instruction, I'm unable to execute the query in Qt. Is it because Qt doesn't support it or is it due to some other reason?
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #4

      HI,

      I think you Query has a bad syntax and it's strange you say it works in SQlite. Are you sure of that?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #5

        Hi,

        Did you check the error message from the model ?

        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
        • H Offline
          H Offline
          holygirl
          wrote on last edited by
          #6

          mcosta

          Yes I've tried it in SQlite and it works very well.

          SGaist

          There's no particular error message. The app runs but I don't see the UI window. That's when I stop the app and it gives exited with code 62097 in the output pane

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #7

            If you don't set the query, does you ui show ?

            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
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #8

              bq. Yes I’ve tried it in SQlite and it works very well.

              What is the result? I think your query has a incorrect syntax.

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • H Offline
                H Offline
                holygirl
                wrote on last edited by
                #9

                SGaist

                Yes the UI shows if my query is something like

                @
                select name from channel
                @

                That's why I was wondering if Qt supports complex SQL queries.

                mcosta

                The result is the name, title and start_time fields like

                NAME | TITLE | START_TIME

                ESPN | NBA Basketball | UTC Time
                ESPN | Up to the minute | UTC Time
                Family Net | The cry of the owl | UTC Time

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  In that case, print what your model.lastError().text() returns. It should give you an idea why it doesn't work.

                  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
                  • H Offline
                    H Offline
                    holygirl
                    wrote on last edited by
                    #11

                    Hi SGaist

                    The lastError returns 0. I tried a variation of the code I originally posted and it worked. Here's the code

                    @
                    Select d.title
                    From channel a
                    Join BROADCAST_SCHEDULE b on b.channel_id = 30
                    Join CONTENT_INSTANCE c on b.instance_id =c.instance_id
                    Join CONTENT d on d.content_id = c.content_id
                    Group By b.start_time
                    @

                    However, I needed to use a where clause to specify a condition. But when used with this clause, the result is nil. There's no error either.

                    @
                    Select d.title
                    From channel a
                    Join BROADCAST_SCHEDULE b on a.channel_id = b.channel_id
                    Join CONTENT_INSTANCE c on b.instance_id =c.instance_id
                    Join CONTENT d on d.content_id = c.content_id
                    where a.name like "HBO"
                    Group By b.start_time
                    @

                    Does Qt not support where clauses? Where am I going wrong? Thanks for your help.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      How do you write the last one in your code ?

                      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
                      • H Offline
                        H Offline
                        holygirl
                        wrote on last edited by
                        #13

                        Like this

                        @
                        QSqlQuery *query = new QSqlQuery();
                        QString title = "HBO";
                        query->prepare (Select d.title
                        From channel a
                        Join BROADCAST_SCHEDULE b on a.channel_id = b.channel_id
                        Join CONTENT_INSTANCE c on b.instance_id =c.instance_id
                        Join CONTENT d on d.content_id = c.content_id
                        where a.name like %1
                        Group By b.start_time).arg(title);
                        query->exec();
                        @

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          This will lead to:

                          @where a.name like HBO@

                          and IIRC it's not a valid like statement

                          try with:

                          @where a.name like '%1'@

                          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
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            Another thing, is HBO the complete name ? Or is it in the name ? Like "something HBO" ?

                            In the later case you have to add % to your like statement, see "this":http://www.w3schools.com/sql/sql_like.asp

                            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
                            • H Offline
                              H Offline
                              holygirl
                              wrote on last edited by
                              #16

                              Thanks SGaist for the w3schools.com link. It was very useful to me to learn how to find a particular pattern from a column in the database. Anyhow, pertaining to the problem I was trying to solve, I used

                              @
                              where a.name like '%1'
                              @

                              instead of

                              @
                              where a.name like %1
                              @

                              and it works perfectly well. Thank you very much for correcting my syntax. I didn't know the single quotes were necessary. Thanks again :)

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                You're welcome !

                                SQL syntax is very sensitive to these kind of details

                                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

                                • Login

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