简体   繁体   English

将表从Microsoft SQL Server转换为R中的数据框

[英]Convert table from Microsoft SQL Server to dataframe in R

I am working on a project and would like to connect to a database on MSSQL Server and then get one(or more) of the tables on the database and convert it/them to a dataframe to work in memory, then I will apply some changes to this dataframe and send it back to the MSSQL Server Database. 我正在一个项目上,想连接到MSSQL Server上的数据库,然后获取数据库中的一个(或多个)表并将其/转换为数据帧以在内存中工作,然后我将应用一些更改到此数据帧,然后将其发送回MSSQL Server数据库。

I am able to connect to my database and I know how to send a dataframe to a database but I don't know how to convert the table to a dataframe as the first step. 我能够连接到数据库,并且知道如何将数据帧发送到数据库,但是作为第一步,我不知道如何将表转换为数据帧。

 con <- dbConnect(odbc(), Driver = "SQL Server", Server = "MyServer", Database = "MyDB", Trusted_Connection = "True") 

You can use SQLdf and SQL query as well to get your desired output (to get the data from a database and convert into a dataframe in R) 您还可以使用SQLdf和SQL查询来获得所需的输出(从数据库中获取数据并转换为R中的数据框)

library(odbc)
library(RODBC)
library(sqldf)

conn <- odbcDriverConnect('driver={SQL Server};server= 
YOURserver;database=Yourdatabase;trusted_connection=true')

DataSQL <- sqlQuery(conn,"SELECT *  FROM dbo.practicR;"); 

View(DataSQL) 

Output: This is the same data in SQL server. 输出:这是SQL Server中的相同数据。

Output 产量

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

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