[英]Unable to execute JDBC statement in sqldf in R
我在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
您可能在運行問題中的代碼之前已加載RH2 R軟件包,因此sqldf使用的是H2數據庫而不是SQLite。
如果您打算使用H2,請注意它不支持USING
關鍵字。 有關H2語法,請參見http://www.h2database.com 。
如果您不打算使用H2,則(i)不加載它,否則(ii)如果您需要加載它,則通過drv = "SQLite"
明確指定sqlite數據庫作為sqldf
的參數之一,否則(iii)指定適當的全局選項: options(sqldf.driver = "SQLite")
。
還要注意,不需要library(tcltk)
行。
錯誤消息模糊列名稱“ CustomerId”意味着數據庫服務器有兩個或多個包含CustomerId
列的表,並且它不知道使用哪個表來滿足select
-clause。 您需要明確指定要使用的表:
SELECT df1.CustomerId, Product, State
FROM df1
JOIN df2 USING(CustomerID)
盡管對於只出現在一個表中的列沒有必要,但最好對每一列都執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.