简体   繁体   English

如何使用 R 将多个 csv 文件上传到 SQL Server 数据库

[英]How to upload multiple csv files to a SQL server Database using R

I have a number folders that store >50 different csv files in each folder, each with different names, different number of variables, and lengths.我有许多文件夹,每个文件夹中存储 >50 个不同的 csv 文件,每个文件夹具有不同的名称、不同数量的变量和长度。 I have to upload them to a SQL server (SSMS) and instead of having to upload them one by one I would like to ask how to batch upload them all at once, especially their unique names (ie Demographics, Biologic, Sites, Labs, OthLabs, Subjects, etc.) and they are not big (under 5mb per folder).我必须将它们上传到 SQL 服务器(SSMS),而不是一个一个地上传它们,我想问一下如何一次批量上传它们,尤其是它们的唯一名称(即人口统计、生物学、网站、实验室、 OthLabs、Subjects 等),它们并不大(每个文件夹不到 5mb)。 I've tried creating a list csv <- dir(pattern = "csv") but haven't figured out how to use it with DBI::dbWriteTable(conn = con, name =, value =, overwrite = FALSE) .我已经尝试创建一个列表csv <- dir(pattern = "csv")但还没有弄清楚如何将它与DBI::dbWriteTable(conn = con, name =, value =, overwrite = FALSE)一起使用。 I would be very grateful if someone has dealt with this in the past could help me.如果过去有人处理过这个问题可以帮助我,我将不胜感激。

Update更新

the tables in question are all different, having different dimensions and lengths (ie(130,12), (151,33), (1,6), (603, 16), etc.), what i'm trying to do is to upload them with their names.有问题的表格都是不同的,具有不同的尺寸和长度(即(130,12),(151,33),(1,6),(603, 16)等),我正在尝试做什么是用他们的名字上传他们。 I've tried the code bellow我试过下面的代码

alldat <- lapply(csv, read.csv) 
for (i in alldat) {
dbWriteTable(conn = con,
 name = "i",
 value =  i,
 overwrite = FALSE)
  }

yes it works, but will create only 1 table called "i", any recommendation on what should the arguments for name = and value = be so that the loop will continue copying the tables from R to SQL server with the table names?是的,它可以工作,但只会创建一个名为“i”的表,关于name =value =的参数应该是什么的任何建议,以便循环将继续将表从 R 复制到 SQL Server,并使用表名?

other functions that I've tried unsuccessfully are list2env , assign do.call(rbind, i)我尝试不成功的其他功能是list2envassign do.call(rbind, i)

Like @r2evans I do this using DBI, rather than dbplyr (I often use the two packages together).像@r2evans 一样,我使用 DBI,而不是 dbplyr(我经常将这两个包一起使用)。

Below is a cut down version of the custom function I use.下面是我使用的定制 function 的精简版。 You can find the full version here .您可以在此处找到完整版本。

copy_r_to_sql <- function(db_connection, db, schema, sql_table_name, r_table_name) {

  suppressMessages( # mutes translation message
    DBI::dbWriteTable(
      db_connection,
      DBI::Id(
        catalog = db,
        schema = schema,
        table = sql_table_name
      ),
      r_table_name
    )
  )
}

For uploading multiple tables, either loop through them or apply .要上传多个表,请遍历它们或apply

So, after finally trying my hand on writing functions and a loop this is what I came up with.因此,在最终尝试编写函数和循环之后,这就是我想出的。 thanks to @Simon.SA and @r2evans for their input感谢@Simon.SA 和@r2evans 的输入

library(dbi)

# Connect to the SQL Server database
con <- DBI::dbConnect(odbc::odbc(),
                      driver = "SQL Server",
                      server = "server_name",
                      database = "database_name",
                      uid = "username",
                      pwd = "password")

# Set the path to the folder containing the CSV files
folder <- "C:/path/to/folder"

#Get a list of the CSV files in the folder
csv_files <- list.files(folder, pattern = "\\.csv$")`

Loop through the CSV files
for (csv_file in csv_files) {
  # Read the CSV file into a data frame
  df <- read.csv(file.path(folder, csv_file))
  
  # Get the name of the table to import the data into
  table_name <- gsub("\\.csv$", "", csv_file)
  
  # Import the data into the SQL Server database
  dbWriteTable(con, table_name, df)
}

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

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