hi folks I can connect to oracle through R no problem using the following code:
library(RODBC)
channel <- odbcConnect(dsn = "xxxx", uid = "xxxx", pwd = "xxxx")
odbcGetInfo(channel)## CHECKS CONNECTION TO ORACLE
COMPANIES <- sqlFetch(channel, "COMPANIES")
COMPANIES_EQUIPMENT <-sqlFetch(channel, "COMPANIES_EQUIPMENT")
EQUIPMENT_SENSORS <-sqlFetch(channel, "EQUIPMENT_SENSORS")
odbcClose(channel) ## CLOSES odbc CONNECTION
when I fetch the first data table "COMPANIES" , no issue but this means running the code just to fetch this data frame, the problem is that when I run the above code to fetch all 3 data frames:
COMPANIES,
COMPANIES_EQUIPMENT,
EQUIPMENT_SENSORS
my R script just hangs up, I have tried to run each fetch statement individually, and they all work but when run together my script just hangs up any ideas?
Not sure if problem is R, New laptop or Oracle, oracle seems ok as can connect no issue but is there a data limit maybe allowed etc...
I am using Oracle Instantclient 11.2 to connect my laptop windows 7 Professional to Oracle, RStudio Version 1.0.143
thanks
Nigel
Have you considered using sqlQuery instead of sqlFetch?
COMPANIES <- sqlQuery(channel, "SELECT * FROM COMPANIES")
you might need to replace the * by the names of the variables.
I personnally use RJDBC to connect to Oracle:
driverClass="oracle.jdbc.OracleDriver"
classPath="<PATH_TO_INSTANTCLIENT>/instantclient_12_1/ojdbc6.jar"
connectPath="jdbc:oracle:thin:#//<HOST>:<PORT>/<DB_NAME>"
jdbcDriver <- RJDBC::JDBC(driverClass, classPath)
jdbcConnection <- RJDBC::dbConnect(jdbcDriver, connectPath, dbuser, dbpass)
Related
I want to connect R to SQL Server so I can export some R data frames as tables to SQL Server.
From a few online tutorials, I've seen they use the RODBC package, and it seems that you first need to create an ODBC name first, by going to ODBC Data sources (64-bit) > System DSN > Add > SQL Server Native Client 11.0> and then insert your specifications.
I have no idea how databases are managed, so forgive my ignorance here.. my question is: if there is already a database/server set up on SQL Server, particularly also where I want to export my R data to, do I still need to do this?
For instance, when I open Microsoft SQL Server Management Studio, I see the following:
Server type: Database Engine
Server name: example.server.myorganization.com
Authentication: SQL Sever Authentication
Login: organization_user
Password: organization_password
After logging in, I can access a database called "Organization_Division_DBO" > Tables which is where I want to upload my data from R as a table. Does this mean the whole ODBC shebang is already setup for me, and I can skip the steps mentioned here where an ODBC needs to be set up?
Can I instead use the code shown here:
library(sqldf)
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "example.server.myorganization.com",
Database = "Organization_Division_DBO",
UID = "organization_user",
PWD = "organization_password")
dbWriteTable(conn = con,
name = "My_R_Table",
value = ) ## x is any data frame I have in R
I note that on this page they use a similar code to above (what is port number?) and also there is some mention "that there is also support for DSNs", so I am a little confused. Also, is there any advantage/disadvantage over using the ODBC package over the RODBC package to do this?
I am trying to connect to a MySQL database through RMySQL but get the following error
"Error in .local(drv, ...) :
Failed to connect to database: Error: Unknown database 'XXX'"
Has anyone had a similar issue and was able to resolve it?
Running
macOS High Sierra, Version 10.13.6
MySQL workbench 8.0
RStudio Version 1.1.453
I constructed the SQL driver as follows:
install.packages("RMySQL")
install.packages("dbConnect")
library(DBI)
library(dbConnect)
con <- dbConnect(RMySQL::MySQL(),
dbname = "xxx",
host = "xxx",
port = xxx,
user = "xxx",
password = "xxx")
I've been following Filip Schouwenaars' datacamp course Importing Data in R (https://www.datacamp.com/courses/importing-data-in-r-part-1) and was hoping to establish a connection to the SQL database and create an MySQLConnection object to then run SQL queries from inside R.
The problem is that I get stuck at the very beginning because of the failure to connect to database. In MySQLworkbench, the script opens and looks great. I'm a complete newbie at this, and am wondering whether this may have something to do with the location of the database file itself? Should I be saving it in a specific folder?
PS: I've read through all RMySQL threads on here and could not find a solution; if I missed something, please let me know. This is my first ask on this forum, and I'm both super grateful for the community here but also worried that I missed something, somewhere. THANK YOU for your help.
Solution: I did in fact not have a 'database' (or schema, as they are now called in MySQL), but merely an .sql file. Once I created a database from the file, it worked like a charm!
For other newbies out there, especially in the humanities, this was a very helpful tutorial on how to set up MySQL with R: https://programminghistorian.org/en/lessons/getting-started-with-mysql-using-r
I have been trying to make a connection between R and the SQL server but I keep getting this error msg:
object 'C_RODBCDriverConnect' not found
It seems as if R is trying to find that object but is failing in doing so, anyone have an idea on how I can solve this issue?
Reinstalling R is not an option as it's a work computer and I do not have the rights to do so. Also note that I am using the RODBC package as the odbc package doesn't want to install properly (I kept getting the zero non-exit error msg).
Thanks in advance.
Zachary
Just make sure you make a odbc connection in the odbc data sources and ensure you have done it correctly to the sql server database.
library(RODBC)
#ODBC_1 refer to Database you want to use
con_ref = odbcConnect("name_of_connection")# name of connection is what you used in odbc connection setup
#input data
x <- sqlQuery(con_ref, "select * from db_name")
I am trying to connect R to Teradata and am not sure what the input items are to the RODBC::odbcDriverConnect(). There is a teradataR package, but it is only used with R versions 3 and under, which I neither have nor want to switch to. Below is a list of the input parameters to get ODBCDriverConnect to work. "Connection" I believe is most important. I need to get an address for a driver that I don't even know if I have. This is what I need most help with. How do I get a driver for Teradata to connect to R? IT at my work is not sure how to do this. Also, if anyone knows of another way to connect Teradata to R (some other package?), please let me know.
connection = ""
case
believeNRows = TRUE
colQuote, tabQuote = colQuote
interpretDot = TRUE
DBMSencoding = "",
rows_at_time = 100
readOnlyOptimize = FALSE
Thank you for your help!
I was able to connect R to Teradata using RODBC package. Here is how to do it if you are working on a pc and have a Teradata driver.
Set up DSN:
Go to: control panel-> administrative tools -> Data Sources (ODBC) -> User DSN tab -> click add-> select Teradata driver (or whatever driver you will be using. ie. could be sql) and press finish.
A box will pop up that needs to be filled in. The following fields need to be filled:
Name: Can be any name you would like. I chose TeraDataRConnection, for example.
Name or IP address (DBC name or address): Mine for example is: Databasename.companyname.com. I looked to see how Microsoft access was connected to the database and in doing that, found the DBC address.
Username: username that you use to connect to database.
Password: password use to connect to databases (if you don't put your password in here, you will have to manually type it into R every time you connect.
In R:
Download RODBC package
library(RODBC)
ch=odbcConnect("TeraDataRConnection", uid="USERNAME HERE",pwd="PASSWORD HERE")
If you want to confim you are connected, you can type in this code to see the tables:
ListOfTables=sqlTables(ch,tableType="TABLE")
That's it!
I am able to connect to Teradata and created a Shiny app which reads data from it.
Firstly we need to install RODBC package in our R. Prerequisite of it is R (≥ 4.0.0) version. No admin access is required to upgrade R even in enterprise laptops.
Follow below steps to successfully setup connection.
Create ODBC Data Sources to connect to Teradata. The connection should be either in 64bit or 32bit, depending on R software.
Use below code snippet to get the data into reactive variable
data <- reactive({
ch <- odbcConnect(dsn = "DSNName", uid = "username", pwd = "password")
sqlQuery(ch,paste('select * from emp ')
})
DSNName - Name of DSN connection created
You can use data() to display and use the value stored in it.
Enjoy!
I am very new to shiny and R but using shiny I am trying to connect to a database fetch the data from there. When I try to access my RShiny work on browser continuously I got an error like Cannot allocate a new connection: 16 connections already opened. How can I overcome this error or RShiny only expecting 16 users at a time? I have got another stack post here RStudio Shiny Error mysqlNewConnection maximum of 16 connections but the explanation was not clear on the above URL.
Maybe you open a new DB connection with obj <- dbConnect(...) every time you send a query in your code. You can simply call dbDisconnect(obj) on the object you created to kill the respective connection everytime after your query executed.
Also you can use this function kill all open connections at once:
library(RMySQL)
killDbConnections <- function () {
all_cons <- dbListConnections(MySQL())
print(all_cons)
for(con in all_cons)
+ dbDisconnect(con)
print(paste(length(all_cons), " connections killed."))
}
I'd recommed to write a small function outside shiny that handles the whole opening and closing thing:
library(RMySQL)
sqlQuery <- function (query) {
# creating DB connection object with RMysql package
DB <- dbConnect(MySQL(), user="youruser", password='yourpassword', dbname='yourdb', host='192.168.178.1')
# close db connection after function call exits
on.exit(dbDisconnect(DB))
# send Query to btain result set
rs <- dbSendQuery(DB, query)
# get elements from result sets and convert to dataframe
result <- fetch(rs, -1)
# return the dataframe
return(result)
}
Hope that helps!
Fast solution
If you get this issue, you should first run the following code interactively to disconnect all your connexions to the MySQL database:
lapply(dbListConnections(MySQL()), dbDisconnect)
(Note that you can replace MySQL() by another DBI Driver, if you use another database management system).
Faster solution
Just restart R session : command/ctrl + shift + F10 on RStudio
How to avoid this issue
You need to tell shiny how to disconnect properly. That part depends on the use case. If you start a connexion each time you start shiny, you could add inside server.ui:
session$onSessionEnded(function(){
dbDisconnect(con)
}
Where con is your connexion.
If you start a connexion each time you run a query, you must disconnect immediately after the query has run.
You should also take a look at the pool package which is suggested by the shiny team to manage connexions.
There is also a very helpful section on Shiny Articles on database.
dbDisconnect() was not working in my case. So I had stopped MySQL server from terminal and again started using
sudo service mysql stop
sudo service mysql start
Then I ran the code with using dbDisconnect() now its working for me.