Why QSqlQuery exec("exit") failed?
-
-
You need to give more details on your attempts instead of throwing some questions into the forum.
If you have specific question on performing some queries why they are not working as expecxted, give some sample code to help others to understand.
-
My query function is as following. It can execute general query sentences, but failed when executing "exit".
void mysql::query(const char* queryStr) { if(!thisdb.isOpen()) { QMessageBox::warning(0,"mysql Query Error!","No Connection!"); return; } qDebug()<<"11\n"; QSqlQuery currquery(thisdb); qDebug()<<"22\n"; if(queryResult) delete queryResult; qDebug()<<"33\n"; queryResult =new std::vector<QString>; assert(queryResult!=nullptr); if(!currquery.exec(queryStr)) { queryValid =false; QMessageBox::warning(0,"Sql Error","Sql Not Executed!"); return; } queryValid =true; while(currquery.next()) { queryResult->push_back(currquery.value(0).toString()); } }
-
@flydragon said in Why QSqlQuery exec("exit") failed?:
It can execute general query sentences, but failed when executing "exit".
Because
exit
is not a general query sentence, it's a command for themysql
command line client, it's not part of SQL. -
Best bet is that is not SQL and hence it do not understand it.
:) @kshegunov , the one ms faster :)
-
Hi,
What is your query exactly ?
The exit SQL keyword has a precise use, you can't just throw it around alone.
And if your query fails then you should at least print the error return by QSqlQuery::lastError.
-
Then just close the database connection with QSqlDatabase.
Aren't you mixing the mysql command line keywords and the SQL query language ?
-
@SGaist I'm sure the syntax is right, because I've input the same sentence "exit" or "quit" in the cmd and it works well. But when using QSqlQuery to execute "exit" or "quit", the lastError's text is
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'quit' at line 1 QMYSQL: Unable to execute query". -
So the answer is yes: you are in fact mixing the mysql command line tool keywords with the SQL Language itself.