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
0 answers

Insert Data Frame to DB2 using RJDBC in R

How to insert data Frame into DB2 from R using RJDBC package. I have tried multiple way most them failed. Current method: Converted the df into query using paste. then using dbExecute(conn,req[1]) get the below error ([jcc][10103][10941][4.15.134]…
Praveen DA
  • 358
  • 4
  • 17
2
votes
3 answers

Error in JDBC create table and insert in amazon redshift

I am using amazon redshift with R. I got the connection working. I wanted to create some database tables and insert some data into them through R. However, I ran into some issues while doing so # Set the Java Environment # For 64 bit…
discipulus
  • 2,665
  • 3
  • 34
  • 51
2
votes
0 answers

Data types returned by dbGetQuery

I am using dbGetQuery to fetch and insert data in a SQL server. I am trying to understand if or not dbGetQuery applies some sort of data categorization algorithms by default. The object x <-dbGetQuery(dbhandle, SQL_stored_procedure) returns a…
S_Dhungel
  • 73
  • 5
2
votes
3 answers

RJDBC, Java connection to Oracle database crashing

I have a script that opens with some code to start a connection to an Oracle database, however the code is crashing RStudio as soon as it runs. The exact code was run successfully on another machine previously. The script opens by loading the…
Qaribbean
  • 178
  • 2
  • 3
  • 17
2
votes
1 answer

Hive JDBC connection issues with Kerberos authentication - R

I am trying to connect Hive via JDBC using RJDBC package in R. Hive is configured with Kerberos authentication. i am using CDH 5.6.0. Hive version is 1.1.0. i am using following…
Sam
  • 1,227
  • 3
  • 11
  • 13
2
votes
0 answers

RJDBC connection unreliable

I'm trying to run a Rscript in red hat linux server. Rscript connects and sends query to Oracle DB, using the method dbConnect & dbSendQuery, provided by the package "RJDBC". I have tried connecting many times, and failed in the majority of them…
SHY_90
  • 29
  • 2
2
votes
1 answer

dbHasCompleted always returns TRUE

I'm using R to do a statistical analysis on a SQL Server 2008 R2 database. My database client (aka driver) is JDBC and thereby I'm using RJDBC package. My query is pretty simple and I'm sure that query would return a lot of rows (about 2 million…
frogatto
  • 28,539
  • 11
  • 83
  • 129
2
votes
1 answer

RJDBC connection to Amazon RedShift produces a .jfindClass class not found error

Good morning, I have successfully set up an RStudio environment to connect to my Amazon RedShift database on Windows and Ubuntu, but have not found the same success in OS X. I downloaded the .jar file from Amazon for JDBC and placed it in the…
Jose Cacho
  • 21
  • 1
  • 3
2
votes
0 answers

Getting a "R session aborted" message when trying to connect to vertica using RJDBC

I'm trying to connect R to vertica and I'm getting a fatal error when I create the vDriver variable. Please any help or direction you can give me to resolve this would be really helpful! library(RJDBC) library(plyr) vDriver <-…
ben890
  • 1,097
  • 5
  • 25
  • 56
2
votes
2 answers

RJDBC can't read table from MSSQL server

I'm running R locally and the database sits on a MS SQL server remotely. I do my prep work: require(RJDBC) # initialize the driver drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "C:/files/sqljdbc4.jar") # establish the connection…
Andre
  • 229
  • 4
  • 9
2
votes
1 answer

How to select database column name with a dot in it in R?

The Vertica database table I'm using has a column called: incident.date I connect to it ok: install.packages("RJDBC",dep=TRUE) library(RJDBC) vDriver <- JDBC(driverClass="com.vertica.jdbc.Driver", classPath="C:/Vertica/vertica…
Oleg
  • 303
  • 2
  • 14
2
votes
2 answers

JRI - How locate Errors in R

So basically, I am using Java, JRI (R for Java) and RJDBC (with the help of JRI), which all work out pretty well. Now, I want to make my program as foolproof as possible. Let's say, that the string SQL_command is some kind of rubbish and not really…
The_F
  • 45
  • 4
2
votes
2 answers

R and Java - rJava error on Windows 8.1

I am trying to make a con using rjdbc and rJava packages and i am getting this error: C:\Users\user\dir>Rscript rjdbc.r Loading required package: methods Error : .onLoad failed in loadNamespace() for 'rJava', details: call: inDL(x,…
user1834437
2
votes
0 answers

Current best supported connector from R calling Hive?

Is anyone currently using Rhive or some other way to make simple hive HQL calls from R, possibly with RJDBC or another connector? Specifically, I have an R script on a client and a hive server on a cluster with CDH4. The Rhive project seemed…
Ed Fine
  • 717
  • 1
  • 6
  • 18
2
votes
0 answers

How to implement an In-Memory database accessible from multiple R sessions inside a JVM?

I'm writing a program that spawns multiple R sessions inside a single JVM. I need to share data between these sessions and I'm writing files (RData/txt) on to the disk for now. I did a bit of reading and found out that an in-memory database is the…
jackStinger
  • 2,035
  • 5
  • 23
  • 36