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. Sqlite query

Sqlite query

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlitequery
8 Posts 4 Posters 550 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.
  • A Offline
    A Offline
    apfal
    wrote on 21 Jul 2024, 18:01 last edited by
    #1

    How to build, in QT, a query containing multiple sqlite statements ?
    I have tried with query.exec and statements separated with “ ; “ - but it always failed.
    Thanks in advance

    A 1 Reply Last reply 21 Jul 2024, 20:20
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 21 Jul 2024, 18:03 last edited by
      #2

      It's not supported atm.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A apfal
        21 Jul 2024, 18:01

        How to build, in QT, a query containing multiple sqlite statements ?
        I have tried with query.exec and statements separated with “ ; “ - but it always failed.
        Thanks in advance

        A Offline
        A Offline
        artwaw
        wrote on 21 Jul 2024, 20:20 last edited by
        #3

        @apfal QSqlQuery does not support running multiple queries at once.
        What you can do is (if statements are static) is to wrap them up in QStringList and run one by one in series. You gain additional advantage of being able to check if each has been successful and react/break if not.
        If you construct them dynamically you obviously have to do a little more work but the principal idea remains the same.

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        1
        • A Offline
          A Offline
          apfal
          wrote on 21 Jul 2024, 21:12 last edited by
          #4

          Thank you
          I would like to make, in sequence, queries to create a table, to insert records in that table and finally to drop and rename other tables.
          Can you give me an example of how to do it with QStringList ?

          C A 2 Replies Last reply 22 Jul 2024, 00:38
          0
          • A apfal
            21 Jul 2024, 21:12

            Thank you
            I would like to make, in sequence, queries to create a table, to insert records in that table and finally to drop and rename other tables.
            Can you give me an example of how to do it with QStringList ?

            C Offline
            C Offline
            ChrisW67
            wrote on 22 Jul 2024, 00:38 last edited by
            #5

            Something like:

            QStringList queries = {
            	QStringLiteral("create table foo ( ... )"),
            	QStringLiteral("insert into foo value ( ... )"),
            	QStringLiteral("insert into foo value ( ... )"),
            	QStringLiteral("drop table bar")
            	};
            
            QSqluery query;
            for (const QString &strQuery: queries) {
            	if (!query.exec(strQuery) {
            		qDebug() << "You broke it :(");
            	}
            }
            
            1 Reply Last reply
            4
            • A apfal
              21 Jul 2024, 21:12

              Thank you
              I would like to make, in sequence, queries to create a table, to insert records in that table and finally to drop and rename other tables.
              Can you give me an example of how to do it with QStringList ?

              A Offline
              A Offline
              artwaw
              wrote on 22 Jul 2024, 13:06 last edited by
              #6

              @apfal like @ChrisW67 suggests.

              If you get the strings as a dump from some tool, you can (after making sure each statement occupies one line) attach a text file with queries into the resource, then load it from file and execute line by line. I used to do so when db to be created/deployed was complex, like hundreds of lines. Gives you an advantage when db changes - all you have to do is replace the file and test, instead of search&replacing literals.
              But that's just a separate can of worms, for what you asked the advice above is sound.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • A Offline
                A Offline
                apfal
                wrote on 22 Jul 2024, 16:00 last edited by
                #7

                Using QStringList worked for me
                Thank you again

                A 1 Reply Last reply 22 Jul 2024, 16:02
                1
                • A apfal
                  22 Jul 2024, 16:00

                  Using QStringList worked for me
                  Thank you again

                  A Offline
                  A Offline
                  artwaw
                  wrote on 22 Jul 2024, 16:02 last edited by
                  #8

                  @apfal Happy you got it working. Good luck!

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0

                  2/8

                  21 Jul 2024, 18:03

                  6 unread
                  • Login

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