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
1
vote
1 answer

R and RJDBC: Using dbSendUpdate results in ORA-01000: maximum open cursors exceeded

I have encountered an error when trying to insert thousands of rows with R/RJDBC and the dbSendUpdate command on an Oracle database. Problem can be reproduced by creating a test table with CREATE TABLE mytest (ID NUMBER(10) not null); and then…
user2161065
  • 1,826
  • 1
  • 18
  • 18
1
vote
2 answers

R and connecting to MySQL via JDBC

I'm trying to connect to a MySql db on a remote machine via R, I've tried RODBC and it fails, I've tried RJDBC and it fails : library(RJDBC) msDriver <- JDBC(driverClass="com.mysql.jdbc.Driver",…
1
vote
0 answers

R transformation from matrix to dataframe - utf-8 encoding gets lost

:)) So, I connected to a DB using JDBC conn <- dbConnect(drv, "jdbc:oracle:thin:...", "DJULIA", "..." , DBMSEncoding='UTF-8') After extracting Information from the database, using a SQL Query, I had the problem, that the…
Mustafa
  • 87
  • 1
  • 12
0
votes
0 answers

Cryptic error when attempting the collect() function on a remote database in R

I am trying to run the code below and I got the error at the bottom: prg3 <- + prg2 %>% + select(PGPRDC, + PGAGRP, + PGCAT1, + PGCAT2, + PGCAT3, + PGCAT4, + PGCDAT, + PGRFDT, + …
0
votes
0 answers

Multi-Shiny App Directory - RJDBC Connection Issue

Currently have a multi Shiny App directory that builds to Docker One of the apps (app1) is reliant upon a JDBC connection that gives me SQL Access. The other app (app2) is a dummy app for now that reads a web csv and makes a ggplot. I can run app1…
tjb22
  • 1
  • 1
0
votes
0 answers

Update one value in Oracle database based on a condition with RJDBC

I am trying to update a value in an Oracle database connected using RJDBC (the connectionis fine as I can query the database from within R). I tried the following (and many variants of it): dbSendUpdate(db_iot, "UPDATE IOT_DATA SET IOTFILE = (?) …
arnyeinstein
  • 669
  • 1
  • 5
  • 14
0
votes
0 answers

RJDBC Error when trying to create a connection to SQL Server

I have been using RODBC to do all sort of sql queries, but I ended up having to deal with a query that happens to take a painfully amount of time compared to when I run the query in SQL Server. I found in this post that RJDBC has a better…
Alejo
  • 315
  • 1
  • 10
0
votes
2 answers

Macbook Pro M1 issues connecting to oracle database with R

I am using an M1 MBP running Monterey and trying to connect to an oracle database to pull data into R. Currently I am trying to setup RJDBC but am open to other options if there is something easier to use. When I try to load RJDBC I am…
user3249770
  • 329
  • 2
  • 7
0
votes
1 answer

Which version of Java do I need for this

I'm trying to use R and the R package rjdbc to connect to an Oracle database. Unfortunately, due to my company's strict IT department, every step of the process is kinda complicated, allow me to explain: For every bit of software we install, we…
Joe Crozier
  • 944
  • 8
  • 20
0
votes
0 answers

dBWriteTable cannot write more than one record (with NA) into into SQL Server table

I am using RJDBC and dbWriteTable to write a data.table into an existing SQL Server database table. Here is my sample data: mtcars After I get connection to DB, I am using dbWriteTable to create a DB table "mtcars". dbWriteTable(conn, "mtcars",…
jfz88
  • 11
  • 1
  • 3
0
votes
1 answer

Amazon Redshift: Queries never finish running after period idle

I am working on a new Amazon Redshift database that I recently started. I am experiencing an issue where after I connect to the database, I can run queries without any issue. However, if I spend some time without running anything (like, 5 minutes),…
0
votes
0 answers

Issues pushing data with NA values to Oracle Database with dbWriteTable using R

I am having issues with pushing data with NA values in Oracle using the dbWriteTable(), which comes from R's DBI package. When I try to upload this data frame to the desired oracle database, I get an error message saying: Error in .local(conn,…
0
votes
1 answer

R - handle error when accessing a database

I'm trying to automate data download from db using RJDBC using a for loop. The database im using automatically closes the connection after every 10mins, so what i want to do is somehow catch the error, remake the connection, and continue the loop.…
adl
  • 1,390
  • 16
  • 36
0
votes
1 answer

rjdbc dbSendUpdate ORA-12899 value too large for column

I have a table with a column of length varchar(500) but when I try writing to this table from R, I am presented with an error of that this column exceeds the maximum number of characters, ie (actual: 295, maximum: 255). How do I fix this, or how do…
joeyops
  • 51
  • 5
0
votes
0 answers

How can i fetch huge (2L record) Oracle DB clob field data into R dataframe effectively?

I am using RJDBC package for R to Oracle connection and ojdbc6.jar file. I have a column of clob datatype in which there is HTML encoding as a data and i am trying to fetch that data in r but its not giving me the result its hang up my system. Below…