Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Execute SQL Server script with QProcess
Forum Updated to NodeBB v4.3 + New Features

Execute SQL Server script with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mourad_bilog
    wrote on 24 Oct 2017, 10:34 last edited by
    #1

    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.

    J 1 Reply Last reply 24 Oct 2017, 10:53
    0
    • M mourad_bilog
      24 Oct 2017, 10:34

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Oct 2017, 10:53 last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mourad_bilog
        wrote on 24 Oct 2017, 11:12 last edited by
        #3

        @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.

        J 1 Reply Last reply 24 Oct 2017, 11:25
        0
        • M mourad_bilog
          24 Oct 2017, 11:12

          @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.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 24 Oct 2017, 11:25 last edited by
          #4

          @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"....);
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mourad_bilog
            wrote on 24 Oct 2017, 15:16 last edited by
            #5

            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 ?

            M 1 Reply Last reply 24 Oct 2017, 17:25
            0
            • M mourad_bilog
              24 Oct 2017, 15:16

              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 ?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 24 Oct 2017, 17:25 last edited by
              #6

              @mourad_bilog

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

              1 Reply Last reply
              2
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 24 Oct 2017, 19:50 last edited by
                #7

                Hi,

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  mourad_bilog
                  wrote on 25 Oct 2017, 10:08 last edited by
                  #8

                  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;
                  	}
                  
                  1 Reply Last reply
                  2

                  1/8

                  24 Oct 2017, 10:34

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved