【问题标题】:Error: Cannot pass NA to dbQuoteIdentifier() in sqldf package in R错误:无法将 NA 传递给 R 中 sqldf 包中的 dbQuoteIdentifier()
【发布时间】:2017-11-22 03:10:38
【问题描述】:
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 包运行任何东西时收到的错误消息。昨天运行的相同查询今天不运行,我做错了什么?

【问题讨论】:

标签: r sqldf


【解决方案1】:

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

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

在摸索了一阵子之后,我意识到这个错误是由于一个未完成(未提交)的事务导致寻址的 SQLite 数据库被“锁定”,这与我同时使用 SQLite 浏览器的工作有关。一旦我提交了待处理的事务,问题就消失了。

我想你一定也想通了,因为你的帖子没有后续行动。对于 RSQLite 人员来说,看看他们是否可以在这些情况下返回更有用的错误消息可能会很好。

拉里·亨斯克

【讨论】:

    【解决方案2】:

    我遇到了同样的问题:

    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 包问题列表中。 github.com/rstats-db/RSQLite/issues/230
    • 这个问题描述有点乱,但是关于核心问题是正确的。就我而言,错误是由非常简单的查询引起的,没有任何重复的列:sqldf("select rok from d")。 data.frame d 本身看起来很正常,没问题,没有重复的列名,所以这非常令人困惑,为什么会失败。但是 data.frame d 本身是使用在 select 短语中有重复列的查询创建的 - 所以 data.frame d 本身必须处于某种不健康的内部状态,您看不到(看起来像任何正常数据。框架)。这就是为什么这个问题如此奇怪:-)
    【解决方案3】:

    我也遇到了同样的错误:

        ## 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 &lt;- data.frame(1,2); names(dd) &lt;- c("a", "a"); con &lt;- dbConnect(SQLite()); dbWriteTable(con, "dd", dd) 给出了该错误,而sqldf("select demand, demand from BOD") 则没有。如果您使用 RH2 后端,它将报告 Duplicated column 作为错误,并会识别重复的列名称。
    【解决方案4】:

    循环中的 sqldf 有同样的问题。通过将其放入 data.frame 调用中来解决它:data.frame(sqldf(..))。

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多