简体   繁体   English

如何连接到R中的SQL Server数据库

[英]How do I connect to an SQL server database in R

I'm trying to connect to the SQL Sever database using R but not sure on the details for the query string. 我正在尝试使用R连接到SQL Sever数据库,但不确定查询字符串的详细信息。 I normally use SQL server management studio on SQL Server 2008 and connnect using single sign on. 我通常在SQL Server 2008上使用SQL server management studio并使用单点登录连接。 I found the below example 我找到了下面的例子

myconn <- odbcDriverConnect(connection="Driver={SQL Server 
Native Client 11.0};server=hostname;database=TPCH;
trusted_connection=yes;")

I get the below warning message 我收到以下警告信息

Warning messages:
1: In odbcDriverConnect(connection = "Driver={SQL Server \nNative Client 11.0};server=hostname;database=TPCH;\ntrusted_connection=yes;") :
  [RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect(connection = "Driver={SQL Server \nNative Client 11.0};server=hostname;database=TPCH;\ntrusted_connection=yes;") :
  ODBC connection failed

How do I go about finding the specifics i need? 我如何找到我需要的细节?

I have done this in the past with an odbc named connection that I've already had in place. 我过去已经用odbc命名连接做了这个,我已经有了。 In case you don't know, you can create one in windows by typing into the search prompt 'odbc' and selecting "set up data sources". 如果您不知道,可以通过在搜索提示“odbc”中键入并选择“设置数据源”来在Windows中创建一个。 For example - if you named an odbc connection 'con1' you can connect the following way: 例如 - 如果您将odbc连接命名为'con1',则可以通过以下方式连接:

con<-odbcConnect('con1') #opening odbc connection


df<-sqlQuery(con, "select  *
                         from ssiD.dbo.HOURLY_SALES
                         ") #querying table


close(con)

This works for me. 这适合我。

library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=server_name; Database=table_name;Uid=; Pwd=; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
odbcClose(channel)

Also, see these links. 另外,请参阅这些链接。 RODBC odbcDriverConnect() Connection Error RODBC odbcDriverConnect()连接错误

https://www.simple-talk.com/sql/reporting-services/making-data-analytics-simpler-sql-server-and-r/ https://www.simple-talk.com/sql/reporting-services/making-data-analytics-simpler-sql-server-and-r/

The problem is simpler than this. 问题比这简单。 The big clue is the \\n in the error message. 最大的线索是错误消息中的\\n Something has re-flowed your connection string such that there is now a new-line character in the driver name. 有些东西重新连接了你的连接字符串,现在驱动程序名称中有一个换行符。 That won't match any registered driver name. 这与任何注册的驱动程序名称都不匹配。 Pain and suffering then ensues. 然后疼痛和痛苦随之而来。 Make sure your whole connection string is on a single line! 确保整个连接字符串在一行上!

I often use: driver={SQL Server Native Client 11.0}; ... 我经常使用: driver={SQL Server Native Client 11.0}; ... driver={SQL Server Native Client 11.0}; ...

and it works really well. 而且效果很好。 Much better than having to rely on pre-defined connection names. 比依赖预定义的连接名称要好得多。

First, you need to install the package 'RSQLServer', and all its dependencies. 首先,您需要安装软件包“RSQLServer”及其所有依赖项。 Then execute the following command in RStudio, with relevant parameters: 然后在RStudio中执行以下命令,并带有相关参数:

conn <- DBI::dbConnect(RSQLServer::SQLServer(),
                 server = '<server>', 
                 port = '<port>',
                 properties = list(
                   user = '<user>',
                   password = '<password>'
                 ))

Finally, db_list_tables(conn) gives you the list of tables in the corresponding database. 最后, db_list_tables(conn)为您提供相应数据库中的表列表。

Try another ODBC driver. 尝试其他ODBC驱动程序。 In windows press the "windows" button and then type "odbc". 在Windows中按“窗口”按钮,然后键入“odbc”。 Click the "Data sources (ODBC)" link. 单击“数据源(ODBC)”链接。 Go to the "Drivers" tab to see the available drivers for SQL Server. 转到“驱动程序”选项卡以查看SQL Server的可用驱动程序。 Also - remove the " " spaces after the semicolons in your connection string. 另外 - 删除连接字符串中分号后面的“”空格。 Note - the database property should point to a database name rather than a table name. 注 - 数据库属性应指向数据库名称而不是表名。

This worked for me: 这对我有用:

odbcDriverConnect("Driver=SQL Server Native Client 11.0;Server=<IP of server>;Database=<Database Name>;Uid=<SQL username>;Pwd=<SQL password>")

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

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