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
4
votes
3 answers

Writing and Updating DB2 tables with r

I can't figure out how to update an existing DB2 database in R or update a single value in it. I can't find much information on this topic online other than very general information, but no specific examples. library(RJDBC) …
runningbirds
  • 6,235
  • 13
  • 55
  • 94
4
votes
1 answer

Connect R and Teradata using JDBC

I´m trying to connect R and Teradata using RJDBC. I´ve found this link that has an example using mysql, but i´m nos sure how to do the same with teradata. library(RJDBC) drv <- JDBC("com.mysql.jdbc.Driver", …
marbel
  • 7,560
  • 6
  • 49
  • 68
4
votes
2 answers

Postgres DB can't connect to R with RJDBC

I've been trying to query data from a PostgreSQL DB via R. I've tried skinning the cat with a few different packages (RODBC, RJDBC, DBI, RPostgres, etc), but I seem to keep getting driver errors. Oddly, I never have trouble using the same…
Jason
  • 259
  • 3
  • 14
3
votes
1 answer

R DBI SQL Server: dbWriteTable truncates rows / field.types parameter does not work

I am using a SQL Server database and JDBC and pool with the sqljdbc42.jar driver. Code: library(DBI) library(RJDBC) library(pool) jar.path.ms.sql.driver <- "./www/base/sql_drivers/sqljdbc42.jar" jdbc.sql.driver <- JDBC( driverClass =…
Paul van Oppen
  • 1,443
  • 1
  • 9
  • 18
3
votes
1 answer

Effective way to write table to SAP HANA from R

I have a table (df) of about 50,000 rows and 12 columns to write to SAP HANA. I use the RJDBC library and write row by row as follows: # Returns the sql statement to insert one row build_insert_string <- function(db_output, row) { row_string <-…
NRLP
  • 568
  • 3
  • 16
3
votes
1 answer

dBWriteTable cannot write NULLs into SQL Server table

ISSUE Cannot insert NA values from a data.frame to a DB Table STEPS Read table from SQL server into R data.frame. Table is decimal with some NULL. data.frame is numeric with some NA. dBWriteTable throws the following error Error in .local(conn,…
gmeroni
  • 571
  • 4
  • 16
3
votes
1 answer

Connecting to DynamoDB using R

I would like to connect to DynamoDB with R. My ultimate goal is to create a Shiny App to display data that is stored at DynamoDB and updated frequently. So I need an efficient way to retrieve it using R. The following references give an intuition…
OzanStats
  • 2,756
  • 1
  • 13
  • 26
3
votes
0 answers

Error warnings with dbGetQuery() in RJDBC with Oracle

I am using dbGetQuery() function to query the Oracle and put results into a data frame in R. I have connected to database successfully, and returned data using dbGetQuery() with simple queries. However, I have a query looks like: Query <-…
Xuefei Hou
  • 73
  • 6
3
votes
1 answer

How to display and close all active RJDBC connections

We've adopted RJDBC for it's speed (over RODBC) but are running into issues with ensuring that all open database connections are closed at the end of an R session. The issue is that after batch runs we'll often have 100+ sleeping DB connections. Our…
Abe
  • 156
  • 5
  • 17
3
votes
2 answers

RJDBC: R to Oracle cannot DELETE or DROP TABLE

I'm using RJDBC to connect to a local database. This allows me to make SELECT queries easily using dbGetQuery, and CREATE TABLE using dbWriteTable. However, I cannot figure out a method to DROP TABLE or DELETE or SELECT INTO directly from my R…
Anton
  • 1,458
  • 1
  • 14
  • 28
3
votes
1 answer

Error on dbSendQuery with RPostgreSQL and RJDBC on CentOS 6.5 and not Windows 7

I've run into a strange problem that occurs on my CentOS 6.5 box hosted on AWS that does not occur on my Windows machine. When sending a large dbSendQuery (string with 15,000+ bytes/characters), I get error messages. With smaller queries, it runs…
Ken Yeoh
  • 876
  • 6
  • 11
3
votes
1 answer

Unable to connect cassandra through R

I am trying to follow an example given on "http://www.datastax.com/dev/blog/big-analytics-with-r-cassandra-and-hive" to connect R with Cassandra. Following is my code: library(RJDBC) #Load in the Cassandra-JDBC diver cassdrv <-…
Osman Khalid
  • 778
  • 1
  • 7
  • 22
3
votes
0 answers

Comparing database connectivity performance in r

Our organization uses oracle databases extensively - we have hundreds of thousands of records that we pull into r and then perform statistics. We've been using the RODBC package almost exclusively, but I've just got rOracle to work, and now I'm…
gruvn
  • 692
  • 1
  • 6
  • 25
2
votes
1 answer

Snowflake through R/RJDBC - Unable to retrieve JDBC result set

I have latest Java installed, as checked using Windows command prompt java --version C:\Users\sweepydodo>java --version java 17.0.1 2021-10-19 LTS Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39) Java HotSpot(TM) 64-Bit Server VM (build…
Sweepy Dodo
  • 1,761
  • 9
  • 15
2
votes
1 answer

ssl connection for RJDBC

My company is instituting an ssl requirement soon for database connections. I previously connected to our Vertica database via DBI and RJDBC packages. I have tried adding an sslmode='require' parameter to my connection. But adding this parameter has…
Joe
  • 3,217
  • 3
  • 21
  • 37
1
2
3
13 14