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
0
votes
1 answer

R: RJDBC: use dbWriteTable with db2 to specify field types

I am trying to write an R data frame to a table in a db2 data base. Does anyone know what additional argument I supply to the function dbWriteTable in order to specify the field types of the columns? I found this similar question: RMySQL…
user32259
  • 1,113
  • 3
  • 13
  • 21
0
votes
0 answers

Writing Data to Oracle DB Server from R

I'm trying to develop a simple R application suing R.NET; gets data from Oracle DB Server, make some modification and write Results to a new Column. I'm using RJDBC to Connect to Oracle DB Server. I couldn't find a way to write data from R…
user168574
  • 15
  • 1
  • 8
0
votes
3 answers

RJDBC Error When Trying to Connect to Hive Server: org.apache.thrift.TApplicationException: Invalid method name: 'execute'

I have been trying to use RJDBC package to connect R(on local machine) with Hive(server), and am seeing errors: Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], : java.sql.SQLException:…
Dai Li
  • 1
  • 2
0
votes
1 answer

R: RJDBC: changing parts of a query via the R console

I suspect this is very simple but couldn't work out how do it, nor find an answer on the internet. I have a table T in a data warehouse which I want to query through the R console e.g. library(RJDBC) query = function(date){ paste('select * from T…
user32259
  • 1,113
  • 3
  • 13
  • 21
0
votes
1 answer

jtds and rjdbc classpath - Error in .jfindClass

I am trying to connect to an SQL server using the jtds driver for JDBC connectivity but the JDBC call in RJDC keeps coming back with an error message. I am using R in Mac OSX 'Error in .jfindClass(as.character(driverClass)[1]) : class not found' I…
Arun
  • 275
  • 1
  • 3
  • 13
1 2 3
13
14