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
QtWS25 Last Chance

Sqlite query

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlitequery
8 Posts 4 Posters 536 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 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

    artwawA 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

        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

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on 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 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 artwawA 2 Replies Last reply
          0
          • A apfal

            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 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

              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 ?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on 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 last edited by
                #7

                Using QStringList worked for me
                Thank you again

                artwawA 1 Reply Last reply
                1
                • A apfal

                  Using QStringList worked for me
                  Thank you again

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on 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

                  • Login

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