[SOLVED] Exec full script by QSqlDatabase
-
The QSqlDataBase::exec() function is just a convenience function to save you having to create a QSqlQuery object yourself. Reading the docs you'll see that it executes a single SQL statement not an entire sql script.
You are responsible for passing the script statements to the QSqlDataBase yourself either using exec() or by creating QSqlQuery objects. Don't forget to check that each was successful before moving onto the next.
-
Tnx, i solve this problem that:
@QSqlQuery query(m_db);
QStringList queryes = script.split(QChar(';'));
foreach(QString queryString, queryes){
query.exec(queryString);
}
@ -
[quote author="Maxim Prishchepa" date="1309855003"]Tnx, i solve this problem that:
@QSqlQuery query(m_db);
QStringList queryes = script.split(QChar(';'));
foreach(QString queryString, queryes){
query.exec(queryString);
}
@[/quote]This only works if you never have a semicolon (';') in your data.
-
Tnx a lot!
I'm rewrite code to this:
@QString queryToExec;
foreach(QString queryString, queryes){
queryToExec += queryString;
if(queryString.endsWith(QChar(')')) == false){
queryToExec += ";";
continue;
}
query.exec(queryToExec);
queryToExec = QString();
}@
I think in current data field never happen a string like ");" -
Ah... :)
Why Qt dose not contains a SQL parser? :)) -
Note that for those corner cases, it may be acceptable to access the API of the underlying driver directly. There may be support for sending multiple statements in one go at that level. Otherwise, perhaps you can look into using Predicate. I believe that that SQL framework does include an SQL parser.