代码之家  ›  专栏  ›  技术社区  ›  EbrahimA

无法在R中的sqldf中执行JDBC语句

  •  1
  • EbrahimA  · 技术社区  · 9 年前

    我在R中使用sqlf合并(连接)两个数据集。但是,我收到一条错误消息:

    Error in .verify.JDBC.result(s, "Unable to execute JDBC statement ", statement) :
    

    下面是一个示例:

    df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
    df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
    library(sqldf)
    library(tcltk)
    df3 <- sqldf("SELECT CustomerId, Product, State 
                  FROM df1
                  JOIN df2 USING(CustomerID)")
    

    运行后,我收到以下错误消息:

    Error in .verify.JDBC.result(s, "Unable to execute JDBC statement ", statement) : 
      Unable to execute JDBC statement SELECT CustomerId, Product, State 
                  FROM df1
                  JOIN df2 USING(CustomerID) (Ambiguous column name "CustomerId"; SQL statement:
    SELECT CustomerId, Product, State 
                  FROM df1
                  JOIN df2 USING(CustomerID) [90059-175])
    

    以下是sessionInfo()之后的输出:

    R version 3.2.0 (2015-04-16)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 8 x64 (build 9200)
    
    locale:
    [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
    [5] LC_TIME=English_United States.1252    
    
    attached base packages:
    [1] grid      tcltk     stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
     [1] sqldf_0.4-10    RSQLite_1.0.0   gsubfn_0.6-6    proto_0.3-10    Hmisc_3.16-0    ggplot2_1.0.1   Formula_1.2-1   survival_2.38-1 lattice_0.20-31 RH2_0.2.3       RJDBC_0.2-5     rJava_0.9-6    
    [13] DBI_0.3.1       chron_2.3-45   
    
    loaded via a namespace (and not attached):
     [1] Rcpp_0.11.6         cluster_2.0.1       magrittr_1.5        splines_3.2.0       MASS_7.3-40         munsell_0.4.2       colorspace_1.2-6    stringr_1.0.0       plyr_1.8.2         
    [10] tools_3.2.0         nnet_7.3-9          gtable_0.1.2        latticeExtra_0.6-26 digest_0.6.8        gridExtra_0.9.1     RColorBrewer_1.1-2  reshape2_1.4.1      acepack_1.3-3.3    
    [19] rpart_4.1-9         stringi_0.4-1       scales_0.2.4        foreign_0.8-63  
    
    2 回复  |  直到 9 年前
        1
  •  0
  •   G. Grothendieck    9 年前

    您可能在运行问题中的代码之前加载了RH2R包,因此sqldf使用的是H2数据库而不是SQLite。

    1. 如果您打算使用H2,请注意它不支持 USING 关键字。看见 http://www.h2database.com H2语法。

    2. 如果您不打算使用H2,则(i)不加载它,或者(ii)如果需要加载它,则通过 drv = "SQLite" 作为 sqldf 或者(iii)指定适当的全局选项: options(sqldf.driver = "SQLite") .

    还要注意 library(tcltk) 不需要行。

        2
  •  0
  •   Mark Rotteveel    9 年前

    错误消息 列名“CustomerId”不明确 表示数据库服务器具有两个或多个包含 CustomerId 列,它不知道使用哪个来满足 select -条款。您需要显式指定要使用的表:

    SELECT df1.CustomerId, Product, State 
    FROM df1
    JOIN df2 USING(CustomerID)
    

    虽然对于只出现在一个表中的列没有必要,但最好对每个列都这样做。