Failure to connect to odbc database in R - r

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

Related

Connect to MySQL database with RMySQL unknown database

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

Julia ODBC connection produces pop-up in Jupyter

This is my first time using Julia and I've written a test script to connect to a database as follows:-
using ODBC
db = ODBC.DSN("DRIVER={SQL Server};SERVER=MyServer;DATABASE=MyDatabase;Trusted_Connection=Yes;");
However, when I execute the code in Jupyter, I get a pop-up each time as shown below. I would like to be able to login automatically using windows authentication and not have to manually enter the login details. Can someone help?
Shoudn't it be True vs Yes:
Trusted_Connection=True;
Also I think you shoud define an ODBC data source in your system and use it's name (e.g. MyDSN) in connection string:
db = ODBC.DSN("DSN=MyDSN;DATABASE=MyDatabase;Trusted_Connection=Yes;");
I found the answer, which is to pass the prompt parameter as follows:
using ODBC
db = ODBC.DSN("DRIVER={SQL Server};SERVER=MyServer;DATABASE=MyDatabase;Trusted_Connection=Yes;";prompt=false);

R connection to SQL Server

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")

RODBC on shinyapps. Error connecting to Access database (.mdb)

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.

What connection string to use to read sqlite db from powerpivot using SQLite ODBC driver

I'd like to import that data contained in a sqlite file to PowerPivot. I downloaded an ODBC driver for sqlite (http://www.ch-werner.de/sqliteodbc/) to accomplish this. In PowerPivot I selected "Home" > "Get External Data" > "From Other Sources". I scrolled down to "Others (OLEDB/ODBC). Selected it and clicked next.
TheFor the connection string. I found this website: http://www.connectionstrings.com/sqlite and I tried the connection string at the bottom suggested for SQLite3 ODBC Driver:
DRIVER=SQLite3 ODBC Driver;Database=c:\Chinook_Sqlite.sqlite;LongNames=0;Timeout=1000;NoTXN=0;
SyncPragma=NORMAL;StepAPI=0;
(I'm using a sample database that I put at the root of my c:. The db if from here: http://chinookdatabase.codeplex.com/releases/view/55169 )
With this connection string when I Test the Connection I get the following error message:
The test connection failed because the provider could not be initialized. If you contact Microsoft support about this error, provide the following message: Faile to connect to server. Reason: Provider information is missing from the connection string. Add the provider information and try again.
I understand that the driver I installed is not found, but I don't know how to correct the connection string to point to the driver dll.
This solution came after many hours of research and trial-and-error. Though it came 2 years late, I am putting it up to help others trying to import information to Power Pivot 2013 from SQLite.
Step 1: Install SQLite ODBC Driver from here.
Step 2: Create a DNS by opening Windows' 'ODBC Data Sources Administrator' (you can find it under Windows > Administrative Tools). See here and here for more information. I have tried creating the DNS under both 'User DNS' and 'System DNS' - both work fine with Power Pivot.
Step 3: Open Power Pivot and do the following:
Click 'From other Sources' > 'Others (OLEDB/ODBC)' > Click on 'Build' button >
Under 'Provider' tab > Select 'MS OLE DB Provider for ODBC Sources' > In 'Use Data Source Name', select your DNS created in Step 2 and add any other parameters. At this point, you can test the connection and it should say 'Test Connection Succeeded'
Once you click 'OK', you should see the Connection String automatically generated. Mine was: 'Provider=MSDASQL;Persist Security Info=False;DSN=SQLiteTest'.
Follow the next few steps to import your data from SQLite.
You need something like this:
Provider=MSDASQL.1;Persist Security Info=False;Mode=ReadWrite;Initial Catalog=C:\XXX.db;DSN=SQLite3 Datasource

Resources