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
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 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 have an R Shiny script that works fine on my local windows computer, but fails when I deploy it to shinyapps.io (which is some unix like).
I originally used:
odbcConnectAccess('test.mdb') but when deployed, I get the follwing error:
Unhandled error in observer: could not find function "odbcConnectAccess"
I tested, and other RODBC functions still work. So I tried this code (based of this post, which also happens to be what the internals of odbcConnectAccess() return)
uid=""
pwd=""
con =paste("Driver={Microsoft Access Driver (*.mdb)};Dbq=", file.path(getwd(),'test.mdb'), ";Uid=", uid, ";Pwd=", pwd, ";", sep = "")
conn=odbcDriverConnect(con)
Again, this works on my local machine, but when deployed to ShinyApps.io I get the following error:
[RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data source name not found, and no default driver specified
I also accidentally tried odbcConnect(con), which gives the following error:
Warning in odbcDriverConnect("DSN=Driver={Microsoft Access Driver (*.mdb)};Dbq=/srv/connect/apps/ALC_app_v0_1/Salvage_data_FTP.mdb;Uid=;Pwd=;") :
[RODBC] ERROR: state IM012, code 0, message [unixODBC][Driver Manager]DRIVER keyword syntax error
In the help for odbcDriverConnect() it mentions that the the format for the connection string depends on your ODBC, and Shinysapps.io seems to use unixODBC, but I cant find what format unixODBC wants me to use so that I can establish the connection?
Can anyone help me figure out how open a connection to a .mdb on shinyapps.io? I'm not sure If I'll be able to alter .ini files on shinyapps.io server, so I'm really hoping to find the correct format/syntax to establish the connection without a DSN.
Thanks for any help or feedback!
"...We currently only support open source databases on shinyapps.io, but we are watching to see if we get a lot more requests for proprietary databases."
From ShinyApps-User group. 10/23/2015
So it looks like the answer is NO currently. [UPDATE SEE BELOW]
Sorry to answer my own question, but I'm going to leave it up since it took me a while to find the confirmation, and it could save others some headache.
[UPDATE]
"Andy corrected me (while on vacation) and let me know that you can make this work by using the FreeTDS driver. I am not an expert on this, but if you change your code to something like this:
odbcDriverConnect("driver=FreeTDS;Server=hostname;database=dbname;uid=username;pwd=password")
We don't have any DSNs configured, but you can see tips for how to do this here:
Trying to connect to an ODBC server using RODBC in ubuntu "
So it seems like it is possible. I'll update if I get it working.
I've been trying to connect my company's DMS to R using the odbcConnect command, but get the following message:
myConn <-odbcConnect("NZSQL", uid="cejacobson", pwd="password")
Warning messages:
1: In odbcDriverConnect("DSN=NZSQL;UID=cejacobson;PWD=password") :
[RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data source name not found, and no default driver specified
2: In odbcDriverConnect("DSN=NZSQL;UID=cejacobson;PWD=password") :
ODBC connection failed
The thing is, I'm positive the Data source name is NZSQL and my uid and password are correct as well. Any insight as to why R may not be finding my data source / driver (the driver is, by the way, specified and working).
How can I fix this?
I ran across this same problem when I was first trying to connect to an Oracle database. In the end what worked for me was using odbcDriverConnect and a connection string instead of odbcConnect.
myConn <-odbcDriverConnect("Driver={Oracle in OraClient11g_home1};Dbq=NZSQL;Uid=cejacobson;Pwd=password;")
You can check on https://www.connectionstrings.com/ for your specific connection string for your database. Mine happened to be this one.
Hope this helps.
This is IM02 error which means name of the DSN is incorrect.
GO to ODBC and check the USER/System DSN that you should be using. Once your name of DSN is correct, you might get IM014 state error which is archtecture mismatch. In that case,
The simpler solution is
IN r studio - go to tools and change the version of R to 32 bit.
It should be ready to work
I was trying to access SQL Server database and got the same error. After using the correct format of db connection, I got access to my sql server database.
dbhandle <- odbcDriverConnect("Driver={SQL Server};Server=mydbhost;Database=mydbname;Trusted_Connection=Yes")
I know this is old but also make sure that you remove the spaces around the '=' sign. That was my problem.
What worked for me was a 32 bit connection instead of a 64 bit connection.
I just spent days on this. If you are using a Microsoft Access database you have to use the full path to the database, it won't even find the file in the same folder.
So from the above question,
myConn <-odbcConnect("NZSQL", uid="cejacobson", pwd="password")
Would need to be something like
myConn <-odbcConnect("c:\\NZSQL", uid="cejacobson", pwd="password")
Phil's link really helped:
https://www.connectionstrings.com/
I had the same problem, what helped me was to add the driver manually:
Search for ODBC Data Source Administrator and click the 64 bit option
Click on "dBASE Files" and then "Add"
Double click on "Oracle in XE"
Fill Data Source Name (NZSQL in your example) and TNS Service Name fields.
Click on Ok.
After that you may go to RStudio again and you will be able to connect using odbcConnect().
I just used R x64 (64-bit) instead of R i386 (32-bit) and it worked