[英]Connecting to MS SQL Server from R on Mac/Linux
I'm trying to connect to a Microsoft SQL Server from R on Mac/Linux, and I have problems with RJDBC.我正在尝试从 Mac/Linux 上的 R 连接到 Microsoft SQL Server,但我遇到了 RJDBC 问题。 When I've downloaded both Microsoft's driver and JTDS, but none of the following lines work:
当我下载了 Microsoft 的驱动程序和 JTDS 时,但以下行均无效:
library(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"/Users/victor/Downloads/sqljdbc_3.0/enu/sqljdbc4.jar")
drv1 <- JDBC('net.sourceforge.jtds.jdbc.Driver',
"/Users/victor/Downloads/jtds-1/jtds-1.3.0.jar")
Each one returns the following error message:每个都返回以下错误消息:
Error in .jfindClass(as.character(driverClass)[1]) : class not found
I suspect the issue may be with the wrong Java version: my default java is我怀疑问题可能出在错误的 Java 版本上:我的默认 java 是
$ java -version
java version "1.7.0_11"
Java(TM) SE Runtime Environment (build 1.7.0_11-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
but I also have Java 1.6 installed on my machine.但我的机器上也安装了 Java 1.6。 I have SquirrelSQL installed and it connects to MS SQL Server without any problem using JTDS driver;
我安装了 SquirrelSQL,它使用 JTDS 驱动程序连接到 MS SQL Server 没有任何问题; the Java version that SquirrelSQL runs under is 1.7.0.11.
SquirrelSQL 运行的 Java 版本是 1.7.0.11。
I have run R CMD javareconf
with the following result:我运行了
R CMD javareconf
,结果如下:
$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.7.0_11
Java home path : /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home/jre
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Java library path:
JNI linker flags : -framework JavaVM
JNI cpp flags : -I$(JAVA_HOME)/include
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
and removed/installed again RJDBC and rJava packages, and still nothing works.并再次删除/安装 RJDBC 和 rJava 包,但仍然没有任何效果。
I guess I'm stuck right now, as I'm not very familiar with Java/RJDBC and their interaction.我想我现在被困住了,因为我对 Java/RJDBC 及其交互不是很熟悉。 Searching google found several people with similar problems but no solution.
搜索谷歌发现有几个人有类似的问题,但没有解决方案。
Any tips on how to make JDBC behave, or any other way to connect to MS SQL Server from R, will be highly appreciated!关于如何使 JDBC 行为的任何提示,或从 R 连接到 MS SQL Server 的任何其他方式,将不胜感激!
UPDATE 1 .更新 1 。 Well, the first statement seems to work now - I'm getting a connection and can query the database without any problems.
好吧,第一条语句现在似乎可以工作了 - 我正在建立一个连接并且可以毫无问题地查询数据库。 Not sure what fixed the problem - may be I needed to restart my mac/R session.
不确定是什么解决了问题 - 可能我需要重新启动我的 mac/R 会话。 The second statement still doesn't work, with the same error message.
第二个语句仍然不起作用,并显示相同的错误消息。
I've been struggling for a while on this.我已经为此苦苦挣扎了一段时间。 Here's what I found.
这是我发现的。
sqljdbc4.jar
.sqljdbc4.jar
。 Use:采用:
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "<wherever sqljdbc4.jar is>")
This should work.这应该有效。
If i'm right, the idea is that for the PATH
variable in drv
function, one will need to specify the PATH where the JDBC driver is located (so if none, then downloaded it FIRST).如果我是对的,想法是对于
drv
函数中的PATH
变量,需要指定 JDBC 驱动程序所在的 PATH (因此,如果没有,则首先下载它)。 Otherwise, one shall receive the common class not find
error.否则,将收到公共
class not find
错误。
The following code achieves your goal of connecting to R from Mac OS x.以下代码实现了从 Mac OS x 连接到 R 的目标。 Download the Microsoft JDBC driver from Microsoft here
在此处从 Microsoft 下载 Microsoft JDBC 驱动程序
Link to Gist / Code in Github链接到 Github 中的要点/代码
# install.packages("RJDBC",dep=TRUE)
library(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver" , "/Users/johndacosta/Downloads/sqljdbc_4.0/enu/sqljdbc4.jar" ,identifier.quote="`")
conn <- dbConnect(drv, "jdbc:sqlserver://192.172.1.210:55158;databaseName=master", "sa", "password")
d <- dbGetQuery(conn, "select * from sys.databases where database_id <= 4 ")
summary(d)
I had this problem with jtds-1.3.1 on Linux.我在 Linux 上使用 jtds-1.3.1 时遇到了这个问题。 The problem disappeared when I tried switching to jtds-1.2.7.
当我尝试切换到 jtds-1.2.7 时,问题就消失了。 It appears there is something in jtds-1.3.* which makes it incompatible with RJDBC.
看起来 jtds-1.3.* 中有一些东西使它与 RJDBC 不兼容。
I had the exact same problem.我有同样的问题。 This is a
jTDS
solution:这是一个
jTDS
解决方案:
~/Downloads/jtds-1.2.8-dist/jtds-1.2.8.jar
.~/Downloads/jtds-1.2.8-dist/jtds-1.2.8.jar
。 Note: Other versions may not work!R
, set up driver: drv <- JDBC("net.sourceforge.jtds.jdbc.Driver", "~/Downloads/jtds-1.2.8-dist/jtds-1.2.8.jar")
.R
设置驱动程序: drv <- JDBC("net.sourceforge.jtds.jdbc.Driver", "~/Downloads/jtds-1.2.8-dist/jtds-1.2.8.jar")
。conn <- dbConnect(drv, "jdbc:jtds:sqlserver://servername:port;DatabaseName=databasename", domain="windows domain", user="user", password="pwd")
.conn <- dbConnect(drv, "jdbc:jtds:sqlserver://servername:port;DatabaseName=databasename", domain="windows domain", user="user", password="pwd")
。 Here the field domain
was messing things up.在这里,领域
domain
事情搞砸了。 You can't put domain\\username
as your user
.你不能把
domain\\username
作为你的user
。 You have to define them separately according to the jTDS driver implementation .您必须根据jTDS 驱动程序实现分别定义它们。
library(RJDBC)
cp <- c
(
"<usr path>/jdbc/mdb/log4j.jar",
"<usr path>/jdbc/mdb/commons_lang.jar",
"<usr path>/jdbc/mdb/commons_logging.jar"
)
.jinit(classpath=cp)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"/Users/victor/Downloads/sqljdbc_3.0/enu/sqljdbc4.jar")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.