I have issues with wrong encoding when connecting to an Oracle database from RStudio. I have tried both the RODBC and ODBC packages.
When I query the database either with sqlQuery() or through dplyr Norwegian letters (Æ, Ø and Å) is not displayed correctly.
I have tried setting the default text encoding i RStudio to UTF-8, and saving the R-scripts with UTF-8 encoding, but the problem persists.
When querying the database with SELECT * FROM NLS_DATABASE_PARAMETERS I find that the database has NLS_CHARACTERSET WE8MSWIN1252, so I have tried to specify this when connecting.
con <- dbConnect(odbc::odbc(), "RStudio", uid = "user",
pwd = "password", encoding = "WE8MSWIN1252")
But this only gives me this error message:
Error in new_result(connection#ptr, statement) :
Can't convert from WE8MSWIN1252 to UTF-8
Any ideas on what I'm doing wrong?
The encoding argument in dbConnect should be set to the encoding used by the system in which you run R, rather than the encoding used in the database (in accordance with the NLS_LANG system environment variable). Therefore, if you run R/Rstudio on a Windows machine, 'encoding = "cp1252"' or 'encoding="latin1"' should do it. However, if you're on Linux or Mac, 'encoding="UTF8" should hopefully work'. If this does not work, make sure that NLS_LANG is set correctly.
(If you use Rstudio server, you have to set NLS_LANG in your .Renviron file, as Rstudio server does not export OS wide environment variables)
Related
I am trying to establish a connection to an Oracle Database in RStudio (Version 2022.07.1 Build 554. Running on SUSE SLE15, SP4).
The connection should by established via an ldap-Server. Its address is in my ldap.ora file which is in /opt/instantclient_19/network/admin.
The code isql my_datasource does establish the connection.
Putting the following code in an R-file an executing via Rscript establishes the connection.
dbConnect(
odbc::odbc(),
dsn = "my_datasource",
UID = "***",
PWD = "***"
)
Trying to source the same script in RStudio (or RStudio Server) throws an error:
Error: nanodbc/nanodbc.cpp:1021: 00000: [Oracle][ODBC][Ora]ORA-12154: TNS:could not resolve the connect identifier specified
There should be a .Renviron file in your home directory. If not, you'll want to create a text file and save it with .Renviron as the filename. In that file, you'll want to define a path for the ODBCINI environmental variable to an ".odbc.ini" or "odbc.ini" file. For example,
ODBCINI = ~/.odbc.ini
The file in this path should list all DSNs you can use to make a connection. For more information, please see https://solutions.rstudio.com/db/best-practices/drivers/.
I have a SQL Server database to which I can connect with my user and this password: "testspecialcharacters_¤" through MS Studio without problems. We are using windows authentication to connect to the server.
However I cannot connect to it through my R Shiny app. I cannot give the exact server, database and user names but this is very close to the code I am using:
library(RODBCext)
ch <- odbcDriverConnect("DSN=test;database=test_db;UID=test_user;PWD=testspecialcharacters_¤")
data <- sqlQuery(ch,"select * from test_db.general.test_tbl")
I receive the following error:
[RODBC] ERROR: state 42000, code 18452, message [unixODBC][FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
[RODBC] ERROR: state 08001, code 0, message [unixODBC][FreeTDS][SQL Server]Unable to connect to data source
ODBC connection failed
I have to mention that the same code works for passwords without that special character. From experience we have found out that "-" and "!" seem to work. However "#" and "¤" do not work. We have not tried all the potential special characters, only these few.
Based on different other posts online I have tried the following, with no success:
ch <- odbcDriverConnect("DSN=test;database=test_db;UID=test_user;PWD=testspecialcharacters_¤", DBMSencoding = "UTF-8")
ch <- odbcDriverConnect("DSN=test;database=test_db;UID=test_user;PWD=testspecialcharacters_¤", DBMSencoding = "UTF-8-BOM")
ch <- odbcDriverConnect("DSN=test;database=test_db;UID=test_user;PWD=testspecialcharacters_¤", DBMSencoding = "latin1")
ch <- odbcDriverConnect("DSN=test;database=test_db;UID=test_user;PWD={testspecialcharacters_¤}")
ch <- odbcDriverConnect("DSN=test;database=test_db;UID=test_user;PWD='testspecialcharacters_¤'")
I am using RStudio Pro with UTF-8 default encoding.
Does anybody know how to escape the special characters used in the password string? Or if a different way of connecting to the database is needed.
The best way is to use basic escaping like this. But you have to test it on your machine, because setting correct encoding between your machine and database is really hard.
x <- "¤"
y <- "\u00A4"
identical(x, y) # true
And link for table
https://www.utf8-chartable.de/
So I'm trying to access my DB file without success. Here is my script:
library(DBI)
library(sqldf)
drv <- dbDriver("SQLite")
con <- dbConnect(drv, dbname = "database.sqlite")
and here is the error:
drv <- dbDriver("SQLite")
con <- dbConnect(drv, dbname = "database.sqlite")
Error in rsqlite_connect(dbname, loadable.extensions, flags, vfs) :
Could not connect to database:
unable to open database file
I've checked, of course, and made sure that I've installed the packages correctly and that my working directory is set.
I have solved my problem, and its a bit embarrassing:
I've saved my file on my desktop. since my OS in installed in my native language (Hebrew) the file path had one Hebrew word in it, and while that doesn't pose a problem with reading tables into R, it does pose a problem to the SQL connection.
solving it was easy - I've saved the file in a new folder on my hard drive (c:\database), set as working dir, and everything worked fine.
I can duplicate this error in two ways:
The file exists but you don't have permission to open it
This could be because of operating system permissions. Check your permissions.
The file doesn't exist and you don't have permission to create it.
If SQLite is asked to open a database file that doesn't exist, it tries to create it. If this fails, you get that error message. This will fail if the path to the DB file (in this case the current working directory) does not let you create files. Check your permissions.
Note that if the file does exist but is corrupted, I get a different error:
> con <- dbConnect(drv, dbname = "database.sqlite")
Error in rsqlite_send_query(conn#ptr, statement) :
file is encrypted or is not a database
>
so that's probably not your problem.
I also had this issue. I double checked the path and spotted the mistake there. Once I provided the correct path, the connection worked.
Thanks for this post. It draw my attention to double check if the path was correct.
I ran into a similar problem and the solution was to use forward slashes when issuing dbConnect. I had a working directory defined as "C:\ ..." which worked fine in the rest of my code but not when I tried to open the file with dbConnect.
Just updated to R 3.0 and updated all the packages, including DBI. To my surprise, a script that I often use stopped working.
I am unable to connect to a MySQL database using dbConnect. The code script instantly, so only a few lines will reproduce the problem
> require("RMySQL")
> m = dbDriver("MySQL")
> dbConnect(m, user = 'user', password = 'pass', dbname = 'dbname', host = 'localhost', client.flag = CLIENT_MULTI_STATEMENTS)
Error in as.integer(from) :
cannot coerce type 'S4' to vector of type 'integer'
Calls: dbConnect ... mysqlNewConnection -> isIdCurrent -> as -> asMethod
Also tried it as:
dbConnect(MySQL(), user = 'user', password = 'pass', dbname = 'dbname', host = 'localhost', client.flag = CLIENT_MULTI_STATEMENTS)
but the same problem
Also tried removing other parameters, but the same issue from the dbDriver.
What changed in the DBI package with the latest update? How can I fix this?
I noticed that the DBI package is orphaned so don't know who to ask.
I had the same issue with R 3.0.1 on ubuntu.
Installing the latest version of the RMySQL-package resolved the problem:
> install.pacakges("RMySQL")
Make sure to restart R after the installation.
I'm still digging into the issue, but I think I've identified multiple causes of this issue. At their root, they all have to do with R expecting an S4 object but getting back an integer instead. I believe these are generally a result of the connection failing to establish.
Why is it failing? One thing I've noticed is that if you fail to close to many of your connections (~16 [see the number of maximum connections specified in the driver handle call] open) DBI won't/can't open a new connection. Make sure you are calling dbDisconnect as needed. Usually, this sort of problem results in a sensible error message, however sometimes results in the above referenced error. If possible access the DB through an abstraction layer, e.g. dplyr as some will monitor the db connections and kill them if they are inactive. Whereas, AFIK if you open a connection in a function and the function breaks, you have no way to close the open connection unless you returned the driver object from your initial call to dbConnect. In this case you have no choice but to restart your instance of R (possibly resetting your machine and clearing your workspace as well).
The other issue I recently encountered is that if RMySQL masks RPostgreSQL, then RPostgreSQL will fail. The reverse does not appear to be the case, but because others have mentioned RPostgreSQL in here as showing the same error message, it seemed worthy of note.
Update
The latest version of RMySQL (0.10.1) seems to have finished off RPostgreSQL - RPostgreSQL now fails to work regardless of load order. The same people working on RMySQL appear to be working on RPostgres (https://github.com/rstats-db/RPostgres) and this conflict appears to be a non-issue if using that package instead of RPostgreSQL. Specifically, use RPostgres::Postgres() in the place of RPostgreSQL::PostgreSQL() when specifying the driver in dbConnect. Other packages, e.g. dplyr, currently assume RPostgreSQL, so this issue can still bite (but it seems a resolution is in the works (https://github.com/rstats-db/RMySQL/issues/28).
I'm trying to demo SQL queries from within R using the Northwind.accdb file. I visited http://office.microsoft.com/en-us/templates/desktop-northwind-2007-sample-database-TC001228997.aspx and was able to download the .accdt file. I've tried creating a database connection using RODBC with the following two lines, but both return the same error that R is unable to find the file, even though I am certain of its location (on the desktop).
Here are the lines of code that I'm using:
conn <- odbcConnectAccess2007("Nwind.accdt", uid = "", pwd = "")
conn <- odbcConnectAccess2007("Nwind.accdb", uid = "", pwd = "")
The error output I'm getting looks like this:
Warning messages:
1: In odbcDriverConnect(con, ...) :
[RODBC] ERROR: Could not SQLDriverConnect
2: In odbcDriverConnect(con, ...) : ODBC connection failed
Very grateful for any pointers you all might have.
There are some problems when using 32 bit drivers on 64 bit Windows. If you have ODBC 32 bit installed, coming with older versions of Office, make sure that you are running 32-bit R (easy with RStudio under Tools).
Or, with 64 bit drivers, use 64 bit R.