You are here:

C++/Algorithm for Database Backup and Restore Programs


Dear Titus‎

There are Database Backup and restore utilities for RDBMS Systems.
The Backup program generates a (.Bak) Binary File as output which can later be restored in case the Database crashes.

What could be the Algorithm/s in database backup/s and restore programs ?. i.e. C/C++ Programming logic/Algorithm.

Awaiting your reply,

Thanks & Regards,
Prashant S Akerkar

If you are asking me what algorithm can be used to backup a database but is written in C++.  I do not know, but that would be reinventing the wheel anyway.  You could think up an algorithm as well as I could that involved reading in all the data in the database into memory then writing that data to a binary flat file.  If you are asking how to run a command that can be run on the command line in C++, then you can use the C++ ::system() function to run a command line command.  The article below suggests this and the details of another method as well :

mysqldump -uroot -pmysql test> C/backup/test.sql

If I run the command above in commandline, it will do a backup for my database "test"

Now, I tried to use the same command inside my Qt C++ code, but it did not work, while I can insert,delete,and update my "test" database easily with no problems.

anyhelp please.

mysql qt
share|improve this question
asked Aug 16 '11 at 11:50

Please paste the code you are trying to do it.  Smar Aug 16 '11 at 11:53
QSqlDatabase db; databaseConnection::openConnection(db); QSqlQuery queryBackup; queryBackup.prepare("mysqldump -uroot -pmysql test > C/backup/test2.sql" ); queryBackup.exec();  Wally Aug 16 '11 at 12:07
add comment
1 Answer
up vote
down vote
You need to use ::system() function to use mysqldump tool. You cannot create dump using SQL queries.

You can use QProcess class for this purpose too.

Here's an example:

QProcess dumpProcess(this);
QStringList args;
args << "-uroot" << "-pmysql" << "test";
dumpProcess.start("mysqldump", args);
Note that your mysqldump tool should be in on any dir in PATH enviroment variable.

share|improve this answer
edited Aug 16 '11 at 12:17

answered Aug 16 '11 at 12:10

Berk Demirkır
Would you add more details please, Berk?  Wally Aug 16 '11 at 12:12
Example would be nice, please.  Wally Aug 16 '11 at 12:13
Updated my answer.  Berk Demirkır Aug 16 '11 at 12:20
It is almost working perfectly, but the problem is that the code generates a test.sql file, but the file size is zero which means it does not connect to my MySQL database and copy the test database. Is there away to add IP address or something like this ? BTW.... I deleted "this" from the 1st line. Cause it gave an error.  Wally Aug 16 '11 at 12:33
this was for parent child relation. It's not important for this example. You need to familiarize yourself with mysqldump command. You need to use proper arguments to get a proper dump.  Berk Demirkır Aug 16 '11 at 12:44


All Answers

Answers by Expert:

Ask Experts


Titus B. Ledbetter, Jr.


C , MFC, Object Oriented, Artificial Intelligence


I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Master's Degree Computer Science from Johns Hopkins

©2017 All rights reserved.