C++/Algorithm for Database Backup and Restore Programs
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.
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
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:
args << "-uroot" << "-pmysql" << "test";
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
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. dev.mysql.com/doc/refman/5.1/en/mysqldump.html – Berk Demirkır Aug 16 '11 at 12:44