Sqlite query
-
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 advance@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. -
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 ?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 :("); } }
-
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 ?@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.