Use Qprocess to back up and restore MySQL database in QT

Source: Internet
Author: User


 Category: Qt2011-02-18 21:35 1395 people read Comments (3) favorite reports Qtmysql database windowspathcmd. Use QT to do MySQL database development, encountered the need to back up, restore the database problem. Instead of providing a SQL statement that backs up the database to a. sql file, MySQL provides a mysqldump.exe tool to do this. Without the SQL statement, Qsqlquery was not used, and decided to use qprocess to implement it. However, using the Qprocess::execute () method in Qt does not lead to data, and more depressing is the QT Command provided in QT Prompt command line tools are prompted to find the Mysqldump.exe program, just at the beginning I suspect that QT is not supported by non-windows/system32 directory instructions outside the command? Search the web for "QT backup MySQL database", the results found the cause of the problem. This article " questions to be aware of when using Qprocess::execute () in Qt"(the original address http://www.qtcn.org/blog/article.php?itemid-629-type-blog.html) writes that the QT program in Windows uses the Qprocess::execute () method, You cannot use pipelines, redirection operators, or redirect operators to redirect content to a file or read input from a file, you need to set the Qprocess object's standard input and output file to the file you need. In general, the command-line programs used in Windows are rarely useful to pipelines and redirects because the Windows console itself does not support pipelines and redirection operations. But this mysqldump is a special case, it needs to redirect the output to a file. But functions like Qprocess::execute ("mysqldump.exe-uusrname-pusrpsd DbName > D:/backup.sql") cannot be performed, at least not in Windows: Starting from Qt4.2, Qprocess provides Setstandardinputfile(), Setstandardoutputfile (), setstandardoutputprocess (). Three functions to handle problems with pipelines and redirects in Windows, replace the Execute () method with the Start () method, and then redirect the output or input using Setstandardoutputfile (). 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);
The corresponding restore database operation should be written like this:
    1. qstring cmd = qstring ( " Mysql.exe -u%1 -p%2 test "). Arg (
    2. qstring path = qstring ( "D://backup". Sql ");   
    3. qprocess *poc=new  qprocess;  
    4. poc->setstandardinputfile (Path);   
    5. poc- >start (CMD);  
Related Article

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.