Execute SQL Server script with QProcess



  • Hello everybody,
    I want to execute an SQL Server script from my Qt 5 application with QProcess.

    QString m_sCommandQuery = QString("cmd.exe /c \"sqlcmd -S %1 -U %2 -P %3 -d %4 -i \"%5\"\"").arg(m_sDbHost, m_sDbUser, m_sDbPwd, m_sDbName, script_path);
    
    QProcess *proc = new QProcess(this);
    	
    	proc->start("cmd.exe", QStringList() << "/c" << m_sCommandQuery);
    					 
    	if (!proc->waitForStarted())
    	{
    		m_pLog->WriteIntoLogFile(QString("Echec d'execution du script %1 : %2").arg(script_path, proc->errorString()));
    		return false;
    	}
    
    	if (!proc->waitForFinished())
    	{
    		m_pLog->WriteIntoLogFile(QString("L'exécution du script %1 n'a pas pu terminé : %2").arg(script_path, proc->errorString()));
    		return false;
    	}
    

    Finaly I will have the follwing command

    cmd.exe /c "sqlcmd -S DESKTOP-GET28BS\SQLEXPRESS -U bilog -P admin -d theriak -i "D:/QtProject/ThUpdater_V2_VS/TheriaUpdater/x64/Debug/LiveUpdate/Scripts/CHNIM_THE_SQLSERVER_1300_V1.3.2.sql""
    

    The problem is that when I lunch the command from Windows start menu it works but when I call it with QProcess nothing happens.
    Can anyone telle me where I'm wrong and what to do to resolve the problem.
    Many thanks in advance.


  • Moderators

    @mourad_bilog Why do you have cmd.exe in m_sCommandQuery and in proc->start?
    Remove it and /c from m_sCommandQuery.



  • @jsulm said in Execute SQL Server script with QProcess:

    m_sCommandQuery

    Thanks for your response.

    It was a mistake beause I've tried some other alternatives.

    I've changed the code to :

    m_sCommandQuery = QString("sqlcmd -S %1 -U %2 -P %3 -d %4 -i \"%5\"").arg(m_sDbHost, m_sDbUser, m_sDbPwd, m_sDbName, script_path);
    
    QProcess *proc = new QProcess(this);
    	
    proc->start("cmd.exe", QStringList() << "/c" << m_sCommandQuery);
    

    but still nothing happens and the script is not executed.


  • Moderators

    @mourad_bilog First, you should always connect slots to http://doc.qt.io/qt-5/qprocess.html#errorOccurred and http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError and print the errors there.
    Second, you should pass all the parameters for sqlcmd as elements in QStringList:

    proc->start("cmd.exe", QStringList() << "/c" << "sqlcmd" << "-S"....);
    


  • I've modified the code as you suggest to be like

    process = new QProcess(this);
    
    	connect(process,SIGNAL(readyRead()),this,SLOT(readStdOut()));
        
    	QStringList argList;
    	argList.append(" /c ");
    	argList.append(" sqlcmd ");
    	argList.append(" -S " );
    	argList.append(m_sDbHost);
    	argList.append(" -U ");
    	argList.append(m_sDbUser);
    	argList.append(" -P ");
    	argList.append(m_sDbPwd);
    	argList.append( " -d ");
    	argList.append(m_sDbName);
    	argList.append(" -i ");
    	argList.append("\"" + script_path + "\"");
    
    	process->start("cmd.exe",  argList);
    

    but nothing happens on my database and there's no error occured. Have you another way to explore ?


  • Qt Champions 2016

    @mourad_bilog

    Hi
    Hook up errorOccurred signal and see if it gives something.
    Also where is sqlcmd located?


  • Lifetime Qt Champion

    Hi,

    On a side note, there's no need for all these spaces around each option.



  • Thanks for your responses.
    I've catched the error and firstly the sqlcmd wasn't knowen instead it's in the variable paths.
    After that, I've changed the sqlcmd by the full path and then I've an error on the -i option and the script path.
    Finaly I've solved the problem by calling sqlcmd as a command and not passing by the command line and replacing "/" by "\" in the script path.

    process = new QProcess(this);
    
    	connect(process,SIGNAL(readyRead()),this,SLOT(readStdOut()));
    	
    	connect(process, static_cast<void(QProcess::*)(QProcess::ProcessError)>(&QProcess::error),
        [=](QProcess::ProcessError error){ 
    		qDebug() << "error enum val = " << error << endl; });
        
    	QStringList argList;
    	argList.append("-S" );
    	argList.append(m_sDbHost);
    	argList.append("-U");
    	argList.append(m_sDbUser);
    	argList.append("-P");
    	argList.append(m_sDbPwd);
    	argList.append( "-d");
    	argList.append(m_sDbName);
    	argList.append("-i");
    	argList.append(script_path.replace("/","\\") );
    
    	process->start("sqlcmd", argList);
    					 
    	if (!process->waitForStarted())
    	{
    		m_pLog->WriteIntoLogFile(QString("Echec d'execution du script %1 : %2").arg(script_path, process->errorString()));
    		return false;
    	}
    
    	if (!process->waitForFinished())
    	{
    		m_pLog->WriteIntoLogFile(QString("L'exécution du script %1 n'a pas pu terminé : %2").arg(script_path, process->errorString()));
    		return false;
    	}
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.