根据列中的因素创建多个 SQL 表

Creating Multiple SQL Tables based on the factors from a column

我正在尝试为 R 数据框中的每个因素创建一个 SQLite table。我已经创建了一个 for 循环来尝试完成此操作,但是当我包含 dWriteTable() 函数时,它给了我一个错误。我相信这可能与“争论”有关,但我不能肯定地说。

这是我目前使用 for 循环的代码:

# Connects to the database##
mydb <- dbConnect(RSQLite::SQLite(), "../Output/all_data.sqlite")

#Reads the selected table in database
mu_ut <- dbReadTable(mydb, "mu_ut")

for(i in unique(mu_ut$AnimalID)){
  AnID <- paste("AnID", i, sep = ".")
  dfname <- assign(AnID, mu_ut[mu_ut$AnimalID == i,])
  dbWriteTable(conn = mydb, name = dfname, value = dat_csv, 
               field.types = c(DateAndTime = "DATETIME", 
                               AnimalID = "CHAR",
                               Species = "CHAR",
                               Sex = "CHAR",
                               CurrentCohort = "CHAR",
                               BirthYear = "DATE",
                               CaptureUnit = "CHAR",
                               CaptureSubunit = "CHAR",
                               CaptureArea = "CHAR"))
}

我在 运行 时收到错误消息:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘dbWriteTable’ for signature ‘"SQLiteConnection", "data.frame", "data.frame"’

如有任何帮助,我们将不胜感激!

谢谢!

使用dbWriteTable时:

  • name 是一个字符串,给出 table.
  • 的名称
  • value 是一个数据框,其中包含要写入 table
  • 的值

尝试:

dbWriteTable(conn = mydb, name = AnID, value = mu_ut[mu_ut$AnimalID == i,],
               field.types = c(DateAndTime = "DATETIME", 
                               AnimalID = "CHAR",
                               Species = "CHAR",
                               Sex = "CHAR",
                               CurrentCohort = "CHAR",
                               BirthYear = "DATE",
                               CaptureUnit = "CHAR",
                               CaptureSubunit = "CHAR",
                               CaptureArea = "CHAR"))