Questions tagged [rjdbc]

RJDBC - Allows the use of JDBC to access databases from R

About

RJDBC is an package providing a database interfaces using . This allows the use of any in R through the JDBC interface. The only requirement is working and a JDBC driver for the database engine to be accessed. RJDBC uses the interface which is also used by the packages , , , and .

Example

library(RJDBC)

drv <- JDBC("com.mysql.jdbc.Driver",
           "/etc/jdbc/mysql-connector-java-3.1.14-bin.jar",
           identifier.quote="`")

conn <- dbConnect(drv, "jdbc:mysql://localhost/test", "user", "pwd")

Loads a JDBC driver for MySQL (adjust the path to the driver's JAR file as necessary) and connects to the local database "test". The connection handle conn is used for all subsequent operations.

(Note: Windows users can use drv<-JDBC("sun.jdbc.odbc.JdbcOdbcDriver") to use Sun's JDBC to ODBC Bridge with "jdbc:odbc:..." URL.)

In order to allow more complex names for tables and columns, it is a good idea to set identifier.quote to the quote character supported by the database for quoting identifiers. The default (NA) does not quote identifiers, but this limits the names that can be used, affecting dbReadTable and dbWriteTable.

dbListTables(conn)
data(iris)
dbWriteTable(conn, "iris", iris, overwrite=TRUE)
dbGetQuery(conn, "select count(*) from iris")
d <- dbReadTable(conn, "iris")

RJDBC supports prepared statements and argument substitution, so it is possible to run queries like:

dbGetQuery(conn, "select count(*) from iris where Species=?", "setosa")

Note that the life time of a connection, result set, driver etc. is determined by the lifetime of the corresponding R object. Once the R handle goes out of scope (or if removed explicitly by rm) and is garbage-collected in R, the corresponding connection or result set is closed and released. This is important for databases that have limited resources (like Oracle) - you may need to add gc() by hand to force garbage collection if there could be many open objects. The only exception are drivers which stay registered in the JDBC even after the corresponding R object is released as there is currently no way to unload a JDBC driver (in RJDBC).

Type Handling

Type-handling is a rather complex issue, especially with JDBC as different databases support different data types. RJDBC attempts to simplify this issue by internally converting all data types to either character or numeric values. When retrieving results, all known numeric types are converted to R's numeric representation and all other types are treated as characters. When assigning parameters in parametrized queries, numeric, integer and character are the types used. Convenience methods like dbReadTable and dbWriteTable can only use the most basic SQL types, because they don't know what DBMS will be used. Therefore dbWriteTable uses only INTEGER, DOUBLE PRECISION or VARCHAR(255) to create the table. For all other types you'll have to use DBML statements directly.

License

RJDBC is released under v2.

Repositories

Other resources

Related tags

200 questions
2
votes
2 answers

RJDBC Query from lists of index values

I am trying to execute a query or the form: SELECT a.col1, b.col2 FROM tbl1 a, tbl2 b WHERE a.col2 = b.col1 AND a.col3 = df$x AND a.col4 = df$y On an Oracle database using RJDBC. I have a data frame with columns x and y which identify. I can't…
Hassantm
  • 563
  • 1
  • 4
  • 14
1
vote
0 answers

SQL loop in R with rjdbc, parameterized query errors

I often run sql queries through R, and I've been working on getting a particular query to work. I provided quite a bit of info about it here in this question. Long story short, I have a data frame I'd already pulled in, and I'm trying to loop…
Joe Crozier
  • 944
  • 8
  • 20
1
vote
0 answers

rJava and RJDBC on Databricks

I am trying to install rJava and RJDBC on Databricks (from notebook). I ran recommended lines: %sh ls -l /usr/bin/java ls -l /etc/alternatives/java ln -s /usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/default-java R CMD javareconf Installation went…
user1700890
  • 7,144
  • 18
  • 87
  • 183
1
vote
1 answer

How can I load RJDBC 0.2.8 if I had R 4.1.3?

I need to know how to install RJDBC 0.2.8 if my R version is 4.1.3. I made this before and it works perfectly, but when I try to do this again, this message appears: Loading required package: DBI Loading required package: rJava Error: package or…
1
vote
1 answer

JDBC Connection to SQL Server using R

I am getting below error while trying to run this script which should connect to MSSQL server library(RJDBC) library(DBI) drv <- RJDBC::JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", …
1
vote
0 answers

Rstudio Connect Shiny dashboard only shows plot if table is visible

I want to have a Shiny dashboard on Rstudio Connect where data is read from my companies data store via JDBC, and then plotted. Ultimately the user would be able to give some parameters and the appropriate output would be displayed. What I've…
1
vote
0 answers

R DBI issue with accessing list fields of a remote table

I am trying to get the fields of a table in an Arctic database. For that I've been successful at creating a jdbcConnection object which is of class "JDBCConnection", but once I write the following code to get the fields of the AR_LOT table…
Diego
  • 392
  • 3
  • 16
1
vote
0 answers

How to set AddVMOption -Doracle.jdbc.thinLogonCapability=o3 or equivalent?

In our organization we are running into the long standing Oracle "unsupported verifier type" bug, well documented since at least the early 2010s. I've been trying to determine how to set the bug fix of AddVMOption…
1
vote
0 answers

Is the R package AWR.Athena, vulnerable to the security issue in log4j?

AWR.Athena package as per CRAN imports RJDBC, rJava. Hence suspecting if it is vulnerable to the recent Apache Log4j security issue. So information on this would be really helpful.
Mahd
  • 11
  • 1
1
vote
1 answer

Connect to Database Using dbPool RJDBC in R

I am trying to use a pool to connect to my database in R, but I get the error: Schema must be specified when session schema is not set How does one specify a schema ? It seems like I need to specify it inside the pool. If that's the case, what's…
shsh
  • 684
  • 1
  • 7
  • 18
1
vote
0 answers

Access H2 database tables with RJDBC

I am trying to access a h2 database with RJDBC package in R. I am able to create a connection, but not able to extract or query the tables. drv <- JDBC("org.h2.Driver", "/jdbc/h2/h2.jar") conn <- dbConnect(drv = drv , …
Crops
  • 5,024
  • 5
  • 38
  • 65
1
vote
1 answer

Save dplyr query to different schema in dbplyr

I have a JDBC connection and would like to query data from one schema and save to another library(tidyverse) library(dbplyr) library(rJava) library(RJDBC) # access the temp table in the native schema tbl(conn, "temp") temp_ed <- temp %*%…
Steve Reno
  • 1,304
  • 3
  • 14
  • 21
1
vote
0 answers

How to change database (e.g. "use my_database") using RJDBC in R-Studio?

I'm having trouble finding the correct command to change databases in R-Studio. I know I can connect to a specific database at the initial connection but I can't seem to find the correct way to execute a "use" command (e.g. "use my_schema"). The…
John
  • 3,458
  • 4
  • 33
  • 54
1
vote
1 answer

How to use R DBI to create a view?

I'm trying to use R's DBI library to create a view on an Athena database, connected via JDBC. The dbSentStatement command, which is supposed to submit and execute arbitrary SQL without returning a result, throws an error when no result set is…
Bob
  • 1,274
  • 1
  • 13
  • 26