错误:在 R 中的 sqldf 包中不能将 NA 传递给 dbQuoteIdentifier()。

11
Error: Cannot pass NA to dbQuoteIdentifier()

此外:警告信息:

In field_types[] <- field_types[names(data)] :
  number of items to replace is not a multiple of replacement length

今天我尝试运行任何使用sqldf包的内容时,会收到这个错误消息。昨天可以正常运行的查询今天却无法运行,我做错了什么吗?


2
请查看 [ask] 和 [mcve]。 - G. Grothendieck
4个回答

8

我也遇到了同样的问题:

Error: Cannot pass NA to dbQuoteIdentifier()
In addition: Warning message:
In field_types[] <- field_types[names(data)] :
number of items to replace is not a multiple of replacement length

经过一些研究,我发现我在一张表中选择了同一列两次:

table1<- sqldf("select columnA,
                       columnA,
                       keyA
                from tableA")
table2<- sqldf("select columnB,
                       keyB
                from tableB")

problematicMerge<- sqldf("select a.*, 
                                 b.* 
                          from tableA a join 
                               tableB 
                          on a.keyA = b.keyB")

这个问题可以通过修改table1表来移除重复列来解决(请参见以下代码:--我认为给其中一个列取别名也可以解决该问题):

table1<-sqldf("select columnA,
                      keyA
               from tableA")

希望这能帮到您。

我已将 #230 问题添加到 RSQLite 包问题列表中。https://github.com/rstats-db/RSQLite/issues/230 - G. Grothendieck
问题描述有点混乱,但核心问题是正确的。在我的情况下,错误是由非常简单的查询引起的,没有任何重复的列:sqldf("select rok from d")。数据框 d 本身看起来很正常,没有重复的列名,所以这让人非常困惑为什么会失败。但是数据框 d 本身是用一个具有重复列的查询创建的 - 所以数据框 d 本身必须处于某种不健康的内部状态,你无法看到它(看起来像任何正常的数据框)。这就是为什么这个问题如此奇怪的原因 :-) - Tomas

1

我也遇到了同样的错误:

    ## step1: encountered the error as below while joining two tables
    screens_temp_2 = sqldf("SELECT a.* , b.ue as 'sp_used_ue' , b.te as 
    'sp_used_te'  from screens_temp a left outer join sp_temp b on  
    a.screen_name = b.screen_name ")
    Error: Cannot pass NA to dbQuoteIdentifier()
    In addition: Warning message:
    In field_types[] <- field_types[names(data)] :
    number of items to replace is not a multiple of replacement length
    ##  step2: while checking the column names , this is what i found
   colnames(screens_temp)
     [1] "screen_name" "usv"         "tsv"         "20_ue"       "20_te"      
    [6] "40_ue"       "40_te"       "60_ue"       "60_te"       "80_ue"      
    [11] "80_te"       "100_ue"      "100_te"      "sp_load_ue"  "sp_load_te" 
   [16] "sp_load_ue"  "sp_load_te" 

上述结果显示 sp_load_ue 和 sp_load_te 重复。
    ## below i corrected the column names:
    colnames(screens_temp) <- c("screen_name", "usv", "tsv", "20_ue", "20_te", "40_ue"   ,    "40_te"   ,    "60_ue"   ,    "60_te"    ,   "80_ue" ,  "80_te"     ,"100_ue"  ,    "100_te"   ,   "sp_load_ue" , "sp_load_te" , "sp_used_ue" , "sp_used_te" )
     write.table(screens_temp, "screens_temp_corrected.csv",  row.names = FALSE ,col.names = TRUE, sep = ",")

    ## again i ran step 1, it worked fine.

注意:我认为sqldf中存在一个错误,它允许在将输出分配给数据框时重复列名。应该在将输出分配给数据框时抛出错误/警告,以便用户可以适当地重命名列。

这种情况发生在dbWriteTable调用中将数据框写入数据库时,而不是读取数据时。即使没有sqldf也会发生这种情况。例如,如果library(RSQLite); dd <- data.frame(1,2); names(dd) <- c("a", "a"); con <- dbConnect(SQLite()); dbWriteTable(con, "dd", dd)出现错误,而sqldf("select demand, demand from BOD")则不会。如果使用RH2后端,它将报告重复列作为错误,并标识重复的列名。 - G. Grothendieck

1

昨天我遇到了同样的问题,当我突然无法将一个表从R上传到我的远程桌面上的SQLite数据库。

lghdb <- dbConnect(SQLite(), 'lgh.db'
dbWriteTable(lghdb, 'SrtrRisks', SrtrRisks)
Error: Cannot pass NA to dbQuoteIdentifier()...

经过一段时间的摸索,我意识到这个错误是由于SQLite数据库被“锁定”,因为存在未完成(未提交)的事务,与我同时使用SQLite浏览器有关。一旦我提交了待处理的事务,问题就消失了。
我想你也一定已经发现了这一点,因为没有对你的帖子进行后续跟进。RSQLite的人们可能会很高兴看到他们是否可以在这种情况下返回更有用的错误消息。
拉里·汉西克尔

1

在循环内部使用sqldf时遇到了相同的问题。通过将其放入data.frame调用中解决了这个问题:data.frame(sqldf(..))。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接