-1

I have a problem with a QSqlQuery when I execute a source command on my database. This code works :

QString myRequest = "CREATE DATABASE MY_DATABASE;";
lQueryResult.Exec(myRequest);
if(!lQueryResult.GetMexec())
{
    qDebug() << "Error in the request";
    return false;
}
else
{
    qDebug() << "The request is OK";
}

This code doesn't works, I have a syntax error :

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'source ./folder/myFile.sql' at line 1 QMYSQL: Unable to execute query

This is the code :

myRequest = "source myFile.sql";
lQueryResult.Exec(myRequest);
if(!lQueryResult.GetMexec())
{
    qDebug() << "Error";
    qDebug() << lQueryResult.LastError();
    return false;
}
else
{
    qDebug() << "OK";
}

I can successfully do my source with this command :

QString lCommand("mysql -uUsername -pPassword Data_Base -e \"source " + variablePath + variableFile + ".sql\"");
        system(lCommandeProvisoire.toStdString().c_str());

I have no error. But, if I execute this code juste after, I have the error

No database selected QMYSQL: Unable to execute query

:

TheRequest = "SELECT * FROM MyTable;";
        QueryResult.Exec(TheRequest);
        if(!QueryResult.GetMexec())
        {
            qDebug() << QueryResult.LastError();
            return false;
        }
        else
        {
            qDebug() << "OK";
        }

But, If I execute a select * from MyTable in shell, it works.

Also, if I execute this in a shell logged in mysql, it works :

source myFile.sql
Anonymous
  • 468
  • 5
  • 26

1 Answers1

1

SOURCE is MySQL client command which means it is executed by MySQL shell, not by MySQL server, so you can't execute it via QSqlQuery. You can work around this by reading whole file and then sending it's contents to server.

  • OK ! There is a mean to backup my database with .sql dump by executing a command wich return a QSqlQuery ? And why if I do a select after the source command my tables are not ready ? But If I do in shell it's ok. – Anonymous Apr 27 '17 at 09:51
  • As I wrote: Instead of executing `SOURCE`, read commands from that file and execute them directly. – el.pescado - нет войне Apr 28 '17 at 05:54