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

R SAP Hana Integration with ODBC for Windows

I would like to connect to SAP Hana Database from RStudio. I've tested several libraries such as: RODBC RJDBC And finally after some readings and research, I've determined that ODBC is the newer library instead of RODBC, and it has better…
2
votes
1 answer

How to connect to Snowflake database through R if MFA (multi factor authentication) is required through Duo app?

I am working on a project which requires me to connect through R to a Snowflake database to pull data from it. However, since we use multifactor authentication to access the DB. I couldnt establish a connection and end up getting timed out. I use…
kgr
  • 23
  • 4
2
votes
1 answer

How to enable SSL in R to connect to Oracle JDBC

I need to connect to Oracle DB via R, how can I enable SSL to connect The usual connection would be like: > install.packages("RJDBC") > library(RJDBC) > drv <-…
Nova Guy
  • 505
  • 2
  • 9
  • 16
2
votes
0 answers

Speeding up parallel SQL querying for R?

I have a dataframe df with an id column. This maps to many rows 1:n) in my database table. Querying each ID sequentially takes about an hour to complete, so I'm trying to run multiple queries at once using the doparallel package. There is overhead…
CorerMaximus
  • 653
  • 5
  • 15
2
votes
1 answer

jni.h file not found

I'm ultimately trying to install the RJDBC package on a mac running R 3.3.3 and Java 12 and Mojave OS. When trying to install RDJBC I get the following error: configure: error: One or more Java configuration variables are not set. Make sure R is…
ben890
  • 1,097
  • 5
  • 25
  • 56
2
votes
1 answer

dbWriteTable function in DBI package is throwing an error

I am trying to use dbWriteTable function in DBI package to upload a dataframe to presto server. It is throwing the following error: "Error in .verify.JDBC.result(r, "Unable to retrieve JDBC tables list") : Unable to retrieve JDBC tables list…
2
votes
3 answers

R: Insert csv-file into database using RJDBC

As RJDBC is the only package I have been able to make work on Ubuntu, I am trying to use it to INSERT a CSV-file into a database. I can make the following work: # Connecting to database library(RJDBC) drv <-…
Esben Eickhardt
  • 3,183
  • 2
  • 35
  • 56
2
votes
1 answer

DBI::dbSendQuery fails with RJDBC::JDBC for SQL SERVER

When I run the insert statement with odbc driver everythings fine. drv <- odbc::odbc() conn <- createConn(drv, trusted_connection = T, dsn="mydsn", uid="myuid", pwd="mypwd") DBI::dbSendQuery("INSERT INTO \"dbo\".\"testjdbc\" (d) values('4')") When…
stakowerflol
  • 979
  • 1
  • 11
  • 20
2
votes
1 answer

Create R Shiny app with dynamic RJDBC connection

I would like to find a similar function to reactiveFileReaderfunction from R Shiny pacakge to dynamically read data from an Oracle database using RJDBC driver. Please find an example below to explain my issue: Suppose my_data.csv is a extraction of…
JeanBertin
  • 633
  • 1
  • 7
  • 23
2
votes
1 answer

Error in .jcall(drv@jdrv, "Ljava/sql/Connection, Cannot connect to database using R

I am trying to access the database using RJDBC from RStudio but I am getting the following error. I am unable to understand my mistake. I will be appreciated if somebody help me on this. Code: install.packages("RJDBC") library(RJDBC) drv <-…
Bhaskar Das
  • 652
  • 1
  • 9
  • 28
2
votes
0 answers

dbGetQuery throttles when the limit is removed for a very large table in a hive database

All, I'm trying to use the packages RJDBC , rJava and DBI in R to extract the data from a big hive table sitting in a mapr hive/hadoop cluster on a remote linux machine. I don't have any issues in connecting to the hive cluster. The table1 I'm…
ML_Passion
  • 1,031
  • 3
  • 15
  • 33
2
votes
1 answer

How does R handle closing of data base connections

If I create a data base connection within a function, the connection objects gets destroyed when the function finished executing. Does this reliably close the data base connection, or would it better to to close it manually first? Why I need to…
Stefan F
  • 2,573
  • 1
  • 17
  • 19
2
votes
2 answers

SYSDATE inside an sql query in R

I am trying to automate a code where I am pulling data for the last week and run it once in a week. How can I get the Sys.Date() inside an sql query in R. library('RJDBC') Tdata<- dbGetQuery(conn, "select roll_id,source,create_date,login…
Lesnar
  • 501
  • 3
  • 16
2
votes
1 answer

Which function to execute grant or synonyms?

I have a synonym statement: CREATE OR REPLACE SYNONYM sample-table-name FOR master.sample-table-name I want to execute this by using R language using RJDBC library. I've tried different functions including dbGetQuery function etc. But it is not…
2
votes
1 answer

how to read data from Cassandra (DBeaver) to R

I am using Cassandra CQL- system in DBeaver database tool. I want to connect this cassandra to R to read data. Unfortunately the connection takes more time (i waited for more than 2 hours) with RCassandra package. but it does not seem to get…
dhinar1991
  • 831
  • 5
  • 21
  • 40
1 2
3
13 14