0

I would like to launch mysql from GUI using QProcess. I've tried the following:

QStringList arguments;
arguments << QString("-u%1").arg("myaccount")<<  QString("-p%2").arg("password");

QProcess *mysql = new QProcess;
mysql->setReadChannelMode(QProcess::ForwardedChannels);
mysql->execute("mysql", arguments);

if(mysql->waitForReadyRead(-1))
    qDebug(mysql->readAllStandardOutput());

But, there is big problem as it's mentioned in Qt Documentation, it's gonna freeze. How can I solve this? Many advised to use QThread but I don't have any idea how to do that? Thanks beforehand!

elgolondrino
  • 665
  • 9
  • 33

2 Answers2

1

The problem is that you call the QProcess::execute() function and it waits until the process is finished. If you need to avoid freezing you can use readyReadStandardOutput() signal and do the following:

[..]
connect(mysql, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
mysql->start("mysql", arguments);
if (!mysql->waitForStarted()) {
    // report error
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Well, Thank you! Also, how can I prompt warning message box when mysql password or user account entered wrong. After connecting readyReadStandardOutput() signal it's gonna be problematic for me to detect this issue. Can you help? – elgolondrino Dec 08 '13 at 15:49
  • @elgolondrino, I think that depends on how `mysql` reports about incorrect username/password input: exits with error code, prints out error message to standard output or standard error? – vahancho Dec 08 '13 at 19:17
0

This link may help you: QProcess fails to execute external executable

Calling MySQL : c:\mysql\mysql.exe -u MYUSERNAME -pMYPassword

There is no space between -p and password. Set MySQL Path.

Community
  • 1
  • 1
Jessica
  • 685
  • 1
  • 9
  • 23