开发者

Dump MySQL database with Qt

I have this slot:

void Managment::dbExportTriggered()
 {
    save = QFileDialog::getSaveFileName(this, trUtf8("Export db"),
                              QDir::currentPath() + "Backup/",
                              trUtf8("Dumped database (*.sql)"));

    sqlQuery = "SELECT * INTO OUTFILE '" + save + ".sql' FROM Users, Data";
    //QMessageBox::critical(0, trUtf8("query dump"), QString::number(query.exec(sqlQuery)));
    query.exec(sqlQuery);
 }

And I have this query:

sqlQuery = "SELECT * INTO OUTFILE " + save + " FROM Users, Data";

I execute normally but no dumped file appear, the backup directory has the right permission, the dumped database must be in client.

UPDATE: After a search I found that the INTO OUTFILE query will dump database in the server not in the client as I was thought, so my question now how can I d开发者_StackOverflowump database in remote MySQL server, any quick methods with out any external tools like mysqldump client.


SELECT ... INTO OUTFILE creates a file on the MySQL server machine, with permissions matching whoever the MySQL server runs as. Unless you have root access on the MySQL server to retrieve the file that you're exporting, SELECT ... INTO OUTFILE is unlikely to do what you want.

In fact, I think I'd go so far as to say that if you're trying to use SELECT ... INTO OUTFILE from a GUI client, you're probably taking the wrong approach to your problem.


Just an idea: Another approach is to call mysqldump with QProcess. With some google-fu this seems to be an example:

..
if (allDatabases->isChecked()) {
    arguments << "--all-databases";
  } else {
    arguments << "--databases";
    foreach(QListWidgetItem *item, databasesList->selectedItems())
      arguments << item->text();
  }
  proc->setReadChannel(QProcess::StandardOutput);
  QApplication::setOverrideCursor(Qt::WaitCursor);
  proc->start("mysqldump", arguments);
..

Thus, you can also add some parameters to dump only a specific table.

Edit:

Just note from the mysql doc on the SELECT ... INTO OUTFILE statement:

If you want to create the resulting file on some other host than the server host, you normally cannot use SELECT ... INTO OUTFILE since there is no way to write a path to the file relative to the server host's file system.

Thus you must roll your own, or you can use mysql -e as suggested by the above documentation.


  1. Did you dump/print save to check it's valid? Does currentPath() return a trailung "/"?
  2. Could there be difference between the path as seen by your client program and as (to be) seen by the server?
  3. Does the user have the necessary privileges (file privilege for sure, maybe more)
  4. Can't you get an error message from the log?


Are you getting any errors running the sql statement?

I notice that you're concatenating the filename into the SQL query without surrounding it by quotation marks. Your code will yield something like

SELECT * INTO OUTFILE /path/to/somewhere FROM Users, Data

but the MySQL documentation says it wants something like

SELECT * INTO OUTFILE '/path/to/somewhere' FROM Users, Data

Also keep the following in mind:

The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.

If you're looking on your client, you won't see the file there, even if the operation succeeds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜