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

How to Connect R to Oracle DB using RJDBC

I am trying to connect R to my Oracle database but keep getting the following error whenever I call the following function: Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], : java.sql.SQLException: NL Exception…
Michael
  • 11
  • 4
0
votes
0 answers

RJDBC: get number of rows inserted

How to get number of rows inserted or updated with RJDBC? Example of such statement where I want get number of rows inserted: conn <- RJDBC::JDBC(#params) DBI::dbConnect(conn) RJDBC::dbSendUpdate(conn, "insert into mytable values(1)") I'm using MS…
stakowerflol
  • 979
  • 1
  • 11
  • 20
0
votes
1 answer

Which format the data should be in to successfully insert it to a table in Netezza DB using RJDBC

I'm trying to insert new rows to an empty table in the DB using dbSendUpdate() from RJDBC package. The fields in the table are as such: integer(10) varchar(255) bigint(19) smallint(5) double(15) The columns in the data.frame…
vladli
  • 1,454
  • 2
  • 16
  • 40
0
votes
1 answer

Amazon Redshift - table columns declared as varchar(max) but forced as varchar(255)

I'm coding a data extraction tool to load data from Google Search Console (GSC from now on) and store it on an Amazon Redshift (AR from now on) database. I coded a function to parse the elements on the data frame coming from GSC to determine the…
Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37
0
votes
0 answers

HANA R Kerberos authentication

I was referring the HANA R connecting document from here. In a username password scenario it works perfect. But in the case of Kerberos what will be the Kerberos parameters for HANA Kerberos authentication?
Jaganadh Gopinadhan
  • 460
  • 1
  • 7
  • 19
0
votes
0 answers

dbReadTable() says "invalid object name" but can select from with dbGetQuery

The following code fails at the second to last line but retrieves the correct result set on the last line: library(RJDBC) drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver","D:/sqljdbc4.jar") conn <- dbConnect(drv, …
A Hood
  • 51
  • 5
0
votes
2 answers

Connect to ORACLE via R, using the info in sql developer

I am working on a machine without admin rights. I use sql developer to connect to an internal database. I would like to connect via R also. Is there any way I can do this, without admin rights? Some solutions require me to set up a systemDNS - which…
Andreas
  • 6,612
  • 14
  • 59
  • 69
0
votes
1 answer

Pasting another variables results into my SQL query in R

So I'm using these three packages in R in order to connect to some databases and query them: library(rJava) library(RJDBC) library (RPostgreSQL) id_query <- dbGetQuery(conn2, "SELECT b.id id FROM table1 a LEFT JOIN table2 b ON a.id = b.id WHERE…
user9302275
0
votes
1 answer

dbWriteTable: Only an ET or null statement is legal after a DDL Statement.)

I am able to read data. However, when writing data to Teradata database, I get the following error: Error in .verify.JDBC.result(s, "Unable to create JDBC prepared statement ", : Unable to create JDBC prepared statement INSERT INTO …
alexsmith2
  • 331
  • 4
  • 12
0
votes
1 answer

R -- RJDBC not able to rename column in SQL select

Using the RJDBC library seems like you can't rename the column in the SQL select. Anyone know a workaround for it? I found this useJDBC4ColumnNameAndLabelSemantics=true for Db2, but not sure it can use it in R. Select a.customer, b.customer as…
newbie_146
  • 127
  • 1
  • 9
0
votes
1 answer

Unable to use RJDBC at Shinyapp.io

I have written a Shiny App which runs perfectly in my local machine. I have used RJDBC to connect to the DB2 database in IBM Cloud. The code is as follows. #Load…
Partha
  • 47
  • 2
  • 8
0
votes
1 answer

Establish JDBC Connection in R through single sign-on

I am currently trying to connect to an Amazon Web Service database through the RJDBC package within R. I have all the information about the driver, jarfile & URL needed in order to connect to the database, however the problem occurs with trying to…
M. Stamp
  • 73
  • 5
0
votes
1 answer

Dplyr and RJDBC : unable to find an inherited method for function ‘dbGetQuery’ for signature ‘"JDBCConnection", "tbl_df"

I'm using RJDBC package for connecting to an Oracle DB. I need to retrieve a huge amount of data, and I would like to distribute the inquiry through different cores. I know that the table has five millions records. Thus I write a SQL Query…
0
votes
0 answers

JDBC insert performance issue in R

I'm facing a performance issue when I insert records in R Script into Teradata database via JDBC. It takes around 100 sec for 100.000 records, which is quite slow considering I have to insert around 10 million records. Does someone any idea how I…
szend
  • 83
  • 8
0
votes
1 answer

dbGetQuery and dbReadTable failing to return a really large table DBI

I have a really large table (8M rows) that I need to import in R on which I will be doing some processing. Problem is when I try to bring it in R using the DBI package I get an error My code is below options(java.parameters = "-Xmx8048m")…