我以 .sql 转储文件的形式将数据库结构存储在我的包的配置文件夹中(存储在 inst/config
中)。
目前我正在使用需要输入密码的命令行:
cat 'filename.sql' | mysql -p databasename
事实上,我是在向用户建议如何使用此函数加载转储:
loaddbstructure <- function(sqlfile = system.file("config/dump.sql",
package="mypackagename", mustWork = TRUE)){
message("This message gives 2 options to load the database structure.\n\n",
"(1) If a user called 'R' is created in MySQL, you can run this from a shell command line: \n",
sprintf("$ cat '%s' | mysql -u R -p tradeflows", sqlfile),
"\n\n",
"(2) Another option is to call this from a mysql client:\n",
"mysql> connect tradeflows;\n",
sprintf("mysql> source %s;", sqlfile))
}
是否可以使用来自 RMySQL 的命令以编程方式加载此 .sql 文件,使用使用 ~/.my.cnf
中提供的凭据创建的数据库连接?
最佳答案
将连接凭据添加到 mysql option file位于 ~/.my.cnf
:
[client]
user = R
password = password
然后您可以使用调用系统 bash 命令(特定于 Linux)的 R 命令加载数据库转储:
system("cat 'dump.sql' | mysql databasename")
关于mysql - 是否可以使用 RMySQL 以编程方式加载 SQL 转储文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45332428/