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

R source file from RConnection (JRI) "could not find function"

I'm trying to source a R script file from an org.rosuda.REngine.Rserve.RConnection and then calling a function from that script, but am getting, "Error: could not find function "main". Start Rserve from terminal > require(Rserve) Loading required…
Dilbert
  • 63
  • 6
0
votes
1 answer

Does anyone know how to connect localhost in R?

drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver","C:/sqljdbc_3.0/enu/sqljdbc4.jar") conn <- dbConnect(drv, "jdbc:sqlserver://.", "Login", "password") But i want to connect my computers local host windows aunthentication so i tried…
bmerv
  • 33
  • 6
0
votes
1 answer

R Shiny: Unable to retrieve JDBC result set for vertica DB

Getting below error while using vertica copy table from local. Please suggest Error:Unable to retrieve JDBC result set for COPY Monetisation_Base_table FROM LOCAL 'E://testCSV.csv' delimiter ',' ([Vertica]JDBC A ResultSet was expected but not…
jacob21
  • 31
  • 7
0
votes
2 answers

Creating JDBC driver in R using RJDBC

I am trying to create a JDBC driver in R using following code: i have already set the directory to location where .jar folder is located drv <- JDBC("com.mysql.jdbc.Driver", "mysql-connector-java-3.1.14-bin-g.jar", …
0
votes
0 answers

SQL queries in R with RJDBC crash if column or table doesn't exist

After creating a simple connection conn to an Oracle database, I write a query like the below: dbGetQuery(conn, "select * from CUSTOMERS where rownum <= 10") Since the table CUSTOMERS exists, this returns a set of rows. However, if I write the…
0
votes
1 answer

dbWriteTable -601 error

I am trying to insert some observations form a R dataframe (R_DATAFRAME) into a db2 dataset (DB2_TABLE) USING dbWriteTable function below dbWriteTable(con1P, "DB2_TABLE", R_DATAFRAME, row.names=FALSE) I am getting an -601 error, i did look up…
King Frazier
  • 243
  • 3
  • 14
0
votes
0 answers

JDBC Connection to Hive version 0.14.0 and from R via RDBC package

I am having hiveserver running on a machine1 Following code I have used for JDBC connection hive_jars <- list.files("/home/jar/hive_jdbc_jar/", pattern = "[.]jar", full.names=FALSE, recursive=TRUE) lib_dir <-…
anban
  • 1
0
votes
0 answers

Create Table in vertica through RJDBC

I saw a similar question in Stackoverflow, but it was not answered. Please let me know if this question is already answered elsewhere. Sorry for the noise. If not, here is my question. I'm connected to Vertica from R through RJDBC. I have no problem…
Hash
  • 11
  • 2
0
votes
0 answers

How to setup several jdbc connectors in one application in order to use them in a loop?

I would like to use several jdbc connections to different MSSQL databases. In order to be able to "loop" through all databases connected, I was trying to set up the jdbc connectors as a list. conn <- list () conn[1] <-…
0
votes
1 answer

Deleting oracle table using RJDBC

Is there a way to delete (truncate) a table in R using oracle function? I am using JDBC to connect to oracle; I would like truncate the contents and then load new data into the table. For instance: select employee_info into variable_name; This…
0
votes
2 answers

R RJDBC oracle dbWritetable, value too large for column maximum: 255

one column of my data frame contains pieces of text, characters is more than 255, when i use dbWritetable to oracle, i got this error: Error in .local(conn, statement, ...) : execute JDBC update query failed in dbSendUpdate (ORA-12899:…
simonsong
  • 9
  • 2
0
votes
1 answer

Loading RJDBC in Shiny app

I have a shiny app running on Ubuntu 14.10 that I need to connect to a SQL Server database. I am using JDBC for the connection. Outside of the app (in the console) I have been able to install the JDBC package and its dependencies, load it, and…
jamarticus
  • 11
  • 5
0
votes
1 answer

RJDBC limiting rows from Netezza

I have an RJDBC connection to Netezza. Queries that should return more than 256 rows are getting truncated to 256 rows. I have tested the queries in SQuirrel and they work fine (return the correct number of rows - 600+). I have also tried the…
briano1945849
  • 101
  • 1
  • 2
  • 9
0
votes
0 answers

RJDBC and orientdb - have connection and see tables but cannot fetch the content

I use the following code to connect orientDB using RJDBC library(RJDBC) drv <- JDBC("com.orientechnologies.orient.jdbc.OrientJdbcDriver","/Users/sofiaapreleva/programs/orien tdb-jdbc-1.7-all.jar", "`") conn <- dbConnect(drv,…
0
votes
1 answer

R: RJDBC: Error to connecting Orientdb

I get error if I try to connect to OrientDB troth Orientdb JDBC in R 3.1.1 and RJDBC 0.2-4 package. The OrientDB is started i am able execute any SQL statments via OrientStudio. JDBC driver is in same directory as OrientDB libs are. Error on…
frankenstein
  • 161
  • 1
  • 13
1 2 3
13
14