Reputation: 7752
I'm having a hard time figuring out how to connect my R code to an embedded H2 database. It seems like RH2 is the tool to go with (library is installed) but there is no working example on the documentation nor on stackexchange or on Google - at least I didn't find one (besides this). Since I'm doing my babysteps with R, a working example to connect and retrieve data would be great!
The code must be something like
library(RH2)
options(RH2.jars = "C:\\h2\\bin\\h2-1.4.187.jar")
myH2 <- H2(driverClass="org.h2.Driver",
identifier.quote="\"", jars = getOption("RH2.jars"))
con <- dbConnect(myH2,
url = "jdbc:h2:C:\\data\\sample.h2.db",
user = "admin", password = "ultrasafe123")
dataFrame = fetch(dbGetQuery(con, "select * from TABLENAME"))
Currently I'm getting:
Error in .jfindClass(as.character(driverClass)[1]) :
class not found` from `driverClass="org.h2.Driver"`
but that's exactly how it is shown in the documentation. Did I miss something else? A library? Including library(RJDBC)
didn't help either.
Upvotes: 0
Views: 1110
Reputation: 7752
I finally solved it for myself:
library(RH2)
myH2 <- H2('org.h2.Driver', 'C:/h2/bin/h2-1.4.187.jar')
## location of h2 file: C:\data\data.h2.db
con <- dbConnect(myH2, "jdbc:h2:C:/data", "user", "password")
s <- "select * from TABLE WHERE COLUMN = 'value'"
result = dbGetQuery(con, s)
dbDisconnect(con)
Upvotes: 3