1

我一直在观察奇怪的行为,DBI:::dbWriteTable这似乎与我试图读入的文件的输入字段中出现单引号有关。

这是一个最小可重现的示例:

library(RSQLite)
con <- dbConnect(RSQLite::SQLite(), dbname = "test_db")

cat("Girls' Shoes\tGirls' Preschool Shoes\nGirls' Shoes\tGirls' Preschool Shoes\nEXPLORER JAKE\tEXPLORER JAKEWITH TOY\n", file = "test.dat")

dbWriteTable(con, name = "test_table", value = "test.dat", head = FALSE, sep = "\t",  field.types = c(V1 = "varchar(25)", V2 = "varchar(25)"))

结果是:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 2 elements

而从每一行中删除一个或两个单引号将导致成功加载:

cat("Girls' Shoes\tGirls Preschool Shoes\nGirls Shoes\tGirls' Preschool Shoes\nEXPLORER JAKE\tEXPLORER JAKEWITH TOY\n", file = "test.dat")

dbRemoveTable(con, name = "test_table")
dbWriteTable(con, name = "test_table", value = "test.dat", head = FALSE, sep = "\t", field.types = c(V1 = "varchar(25)", V2 = "varchar(25)"))

为什么负载无法正确解析单引号?什么是不涉及从原始文件中删除单引号的解决方法?

4

0 回答 0