Use QProcess in Qt to back up and restore the Mysql database

Source: Internet
Author: User
Tags database issues

UseQtDoMySQL databaseDevelopment, encountered the need to back up and restore the database issues.MySQLThere is no SQL statement used to compile the database into A. SQL file, and a mysqldump.exe tool is used to complete this function. Without SQL statements, QSqlQuery cannot be used.QProcess.

HoweverQtUsed inQProcess: The execute () method cannot export data. Even more depressing is that the Qt Command promptcommand tool provided by Qt cannot find the mysqldump.exe program,
At first, I suspected that the command does not support commands other than the Windows/System32 directory commands?
.
Search for "qt back up Mysql Database" online, and find the cause of the problem. This article "notes when using QProcess: execute () in Qt", uses the QProcess: execute () method in Qt programs in Windows, and cannot use pipelines or redirection operators, to redirect the content to a file or read the input from the file, you must set the standard input and output files of the QProcess object as needed.

Generally, the command line programs used in Windows are rarely useful for pipelines and redirection, because the Windows console itself does not support pipelines and redirection operations.

However, this mysqldump is a special case. It needs to redirect the output to a file.

But like

 
 
  1. QProcess::execute("mysqldump.exe -uUsrName -pUsrPsd DbName > d:/backup.sql") 

The function cannot be executed, at least in windows.

QProcess provides setStandardInputFile (), setStandardOutputFile (), and setStandardOutputProcess () starting from Qt4.2 (). three functions are used to handle MPs queue and redirection problems in Windows. Use the start () method instead of the execute () method, and then use setStandardOutputFile () to redirect the output or input.

Specifically, the mysqldump operation should be written as follows:

 
 
  1. QString Cmd = QString("mysqldump.exe --add-drop-table -u%1 -p%2 test").arg("UsrName","UsrPsd");    
  2. QString Path = QString("%1").arg("d://backup.Sql");    
  3. QProcess *poc=new QProcess;    
  4. poc->setStandardOutputFile(Path);    
  5. poc->start(Cmd);    
  6.     QString Cmd = QString("mysqldump.exe --add-drop-table -u%1 -p%2 test").arg("UsrName","UsrPsd");  
  7.     QString Path = QString("%1").arg("d://backup.Sql");  
  8.     QProcess *poc=new QProcess;  
  9.     poc->setStandardOutputFile(Path);  
  10.     poc->start(Cmd); 

The corresponding database restoration operations should be written as follows:

 
 
  1. QString Cmd = QString("mysql.exe -u%1 -p%2 test").arg("UsrName","UsrPsd");    
  2. QString Path = QString("%1").arg("d://backup.Sql");    
  3. QProcess *poc=new QProcess;    
  4. poc->setStandardInputFile(Path);    
  5. poc->start(Cmd);   

Summary: InQtUsed inQProcessBackup and recoveryMysql databaseI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.