Sqlite query
-
wrote on 21 Jul 2024, 18:01 last edited by
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 -
It's not supported atm.
-
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 advancewrote on 21 Jul 2024, 20:20 last edited by@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. -
wrote on 21 Jul 2024, 21:12 last edited by
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 ? -
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 ?wrote on 22 Jul 2024, 00:38 last edited bySomething 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 :("); } }
-
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 ?wrote on 22 Jul 2024, 13:06 last edited by@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. -
wrote on 22 Jul 2024, 16:00 last edited by
Using QStringList worked for me
Thank you again
6/8