简体   繁体   English

使用Qt转储MySQL数据库

[英]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. 我正常执行但没有出现转储文件, backup目录具有正确的权限,转储的数据库必须在客户端。

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 dump database in remote MySQL server, any quick methods with out any external tools like mysqldump client. 更新:搜索后我发现INTO OUTFILE查询会将数据库转储到服务器而不是客户端,因为我想到了,所以我现在的问题是如何在远程MySQL服务器中转储数据库,任何快速方法都没有任何外部工具如mysqldump客户端。

Just an idea: Another approach is to call mysqldump with QProcess . 只是一个想法:另一种方法是使用QProcess调用mysqldump With some google-fu this seems to be an example: 有些google-fu 似乎就是一个例子:

..
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: 请注意SELECT ... INTO OUTFILE语句中的mysql文档

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. 如果要在服务器主机之外的其他主机上创建生成的文件,通常无法使用 SELECT ... INTO OUTFILE,因为无法相对于服务器主机的文件系统写入文件的路径。

Thus you must roll your own, or you can use mysql -e as suggested by the above documentation. 因此,您必须自己滚动,或者您可以使用上面文档中建议的mysql -e

SELECT ... INTO OUTFILE creates a file on the MySQL server machine, with permissions matching whoever the MySQL server runs as. SELECT ... INTO OUTFILE在MySQL服务器计算机上创建一个文件,其权限与MySQL服务器运行的任何一个匹配。 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. 除非您在MySQL服务器上具有root访问权限以检索您要导出的文件,否则SELECT ... INTO OUTFILE不太可能执行您想要的操作。

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. 事实上,我想我甚至会说,如果你试图从GUI客户端使用SELECT ... INTO OUTFILE ,你可能会采取错误的方法解决你的问题。

  1. Did you dump/print save to check it's valid? 您是否转储/打印保存以检查它是否有效? Does currentPath() return a trailung "/"? currentPath()是否返回了一个“/”?
  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? 您是否在运行sql语句时遇到任何错误?

I notice that you're concatenating the filename into the SQL query without surrounding it by quotation marks. 我注意到你将文件名连接到SQL查询而不用引号括起来。 Your code will yield something like 你的代码会产生类似的东西

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

but the MySQL documentation says it wants something like MySQL文档说它需要类似的东西

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权限才能使用此语法。 file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed. file_name不能是现有文件,除其他外,它会阻止/etc/passwd和数据库表/etc/passwd文件被销毁。

If you're looking on your client, you won't see the file there, even if the operation succeeds. 如果您在客户端上查看,即使操作成功,您也不会在那里看到该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM