Only get `character(0)` from ROracle conection - r

Does someone know why I am unable to get data tables from my ROracle connection configured as below?
drv <- dbDriver("Oracle")
con <- dbConnect(drv,username = "myusername", password = "mypassword ")
Up to there it seems to be okay, I get the following items on my environment
seeROracle environment
But then, when I use dbListTables(con) to see which tables I recover, I just got character(0) like there is nothing in my Oracle database.
How can I view the tables?

Related

R - handle error when accessing a database

I'm trying to automate data download from db using RJDBC using a for loop. The database im using automatically closes the connection after every 10mins, so what i want to do is somehow catch the error, remake the connection, and continue the loop. In order to do this i need to capture the error somehow, the problem is, it is not an r error so none of the commands trycatch and similar works. I just get a text on the console telling me:
Error in .jcheck() : No running JVM detected. Maybe .jinit() would help.
How do i handle this in terms of:
if (output == ERROR) {remake connection and run dbQuery} else {run dbQuery}
thanks for any help
You could use the pool package to abstract away the logic of connection management.
This does exactly what you expect regarding connection management with DBI.
It should work with RJDBC which is an implentation of DBI, but I didn't test it with this driver.
libray(pool)
library(RJDBC)
conn <- dbPool(
drv = RJDBC::JDBC(...),
dbname = "mydb",
host = "hostadress",
username = "test",
password = "test"
)
on.exit(poolClose(conn))
dbGetQuery(conn, "select... ")

Connect to MSSQL using DBI

I can not connect to MSSQL using DBI package.
I am trying the way shown in package itself
m <- dbDriver("RODBC") # error
Error: could not find function "RODBC"
# open the connection using user, passsword, etc., as
# specified in the file \file{\$HOME/.my.cnf}
con <- dbConnect(m, dsn="data.source", uid="user", pwd="password"))
Any help appreciated. Thanks
As an update to this question: RStudio have since created the odbc package (or GitHub version here) that handles ODBC connections to a number of databases through DBI. For SQL Server you use:
con <- DBI::dbConnect(odbc::odbc(),
driver = "SQL Server",
server = <serverURL>,
database = <databasename>,
uid = <username>,
pwd = <passwd>)
You can also set a dsn or supply a connection string.
It looks like there used to be a RODBC driver for DBI, but not any more:
http://cran.r-project.org/src/contrib/Archive/DBI.RODBC/
A bit of tweaking has got this to install in a version 3 R but I don't have any ODBC sources to test it on. But m = dbDriver("RODBC") doesn't error.
> m = dbDriver("RODBC")
> m
<ODBCDriver:(29781)>
>
Suggest you ask on the R-sig-db mailing list to maybe find out what happened to this code and/or the author...
Solved.
I used library RODBC. It has great functionality to connect sql and run sql queries in R.
Loading Library:
library(RODBC)
# dbDriver is connection string with userID, database name, password etc.
dbhandle <- odbcDriverConnect(dbDriver)
Running Sql query
sqlQuery(channel=dbhandle, query)
Thats It.

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",
"/etc/jdbc/mysql-connector-java-3.1.14-bin.jar",
identifier.quote="`")
conn <- dbConnect(drv, "jdbc:mysql://localhost/test", "user", "pwd")
I´ve downloaded this driver:
http://downloads.teradata.com/download/connectivity/jdbc-driver
But i´m not sure where i should reference the directory.
I know there is a teradataR package out there, but i don´t know if it really works with the R 3.0.0.
For the time being i´m just interesting in pulling data out of the database. Something as simple as SELECT * FROM table. The problem is RODBC is very slow...
Are there other options for doing this task?
Using the R Console, enter the following steps below to make a Teradata connection:
drv = JDBC("com.teradata.jdbc.TeraDriver","ClasspathForTeradataJDBCDriverFiles")
Example:
drv = JDBC("com.teradata.jdbc.TeraDriver","c:\\terajdbc\\terajdbc4.jar;c:\\terajdbc\\tdgssconfig.jar")
NOTE: A path on a UNIX machine would use single forward slashes to separate its components and a colon between files.
conn = dbConnect(drv,"jdbc:teradata://DatabaseServerName/ParameterName=Value","User","Password")
Example:
conn = dbConnect(drv,"jdbc:teradata://jdbc1410ek1.labs.teradata.com/TMODE=ANSI,LOGMECH=LDAP","guestldap","passLDAP01")
NOTE: Connection parameters are optional. The first ParameterName is separated from the DatabaseServerName by a forward slash character.
dbGetQuery(conn,"SQLquery")
Example:
dbGetQuery(conn,"select ldap from dbc.sessioninfov where sessionno=session")

connecting form R (client) to Greenplum server

I'm trying to retrieve data from greenplum cluster into R (win client).
I've tried:
library("RODBC")
conn <- odbcDriverConnect("DSN_name")
Sql <- "select * from DB.st.country"
cen_data <- sqlQuery(conn,Sql)
print(cen_data)
I'm getting error:
0A000 7 ERROR: cross-database references are not implemented
I have seen some answers about dblink but when I tried:
sql <- "select dblink_connect('conn', 'dbname=myDB');"
cen_data <- sqlQuery(conn,Sql)
I'm getting error:
"42883 7 ERROR: function dblink_connect(unknown, unknown) does not exist
Does anyone have any idea what Am I doing wrong?
Instead of ODBC, you can also use the RPostgreSQL package, which uses DBI as the backend.
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="hostname", user=..., pass=...)
This is not an R issue (syntax is ok).
The problem was in database definitions.
You need to have the database in the "select data source". for that you need to have postgreSQL.

R DBI / RPostgreSQL-- connection succeeds but dbListTables returns no tables

The following code connects to my PostgreSQL database successfully (or appears to, at any rate), but attempt to issue queries were met with "relation does not exist" errors, so I tried dbListTables, which doesn't return any tables at all. The database name passed to dbConnect is correct, and the tables do exist. I think the code I'm using is exactly the same as what I was using recently, which worked successfully. Any ideas?
> library(RPostgreSQL)
Loading required package: DBI
> drv <- dbDriver("PostgreSQL")
> con <- dbConnect(drv, dbname="mydb", user="user", password=password)
> dbListTables(con)
character(0)
I'm new to both R and DBI, so I'm sure I could be missing something extremely simple...any help would be appreciated.
Solved-- I was right; it was something incredibly simple (and very, very stupid) on my part. I was running the script from the wrong server. The server I was running it from has an empty copy of the database I was attempting to connect to, so everything succeeded, and the empty result from dbListTables was correct. Once I switched servers (or simply specified the host on the other server), everything worked.
1.Connet to MySQL
a)if Mysql is installed in your system, if not install it.
b)download the RMySQL IN R
library(RMySQL)
drv = dbDriver("MySQL 5.0.1")
make sure MySQL version is correct.
con = dbConnect(drv,host="localhost",dbname="test",user="root",pass="root")
use local host or use the server i.e ip address
use the required database name, user name and password
album = dbGetQuery(con,statement="select * from table")
run required query
close(con)
2.Another way to connect database
a)first install any database like MySQL,Oracle,SQL Server
b)install the ODBC connector for database
library(Rodbc)
channel <- odbcConnect("test", uid="ripley", pwd="secret")
test is the connection name of odbc conector which user has to set manualy
user can find this in Administrator tool
res <- sqlFetch(ch, "table name")
A table can be retrieved as a data frame
res<-sqlQuery(channel, paste("select query"))
part of the with condition one table can be retrieved as a data frame
sqlSave(channel, dataframe)
to save a dataframe to the database(dont use "res<-" something like this)
like user can use
sqlCopy()
sqlDrop()
sqlTables()
close(channel)
always close the connection

Resources