odbc icon indicating copy to clipboard operation
odbc copied to clipboard

password with special characters in odbc connection string in R

Open alipprc opened this issue 2 years ago • 3 comments

Some of my shiny app users can not login to the database via app but they could from their windows machine via SQL management studio. I'm using the odbc package to create the connection string like :

ch <- odbc::dbConnect(odbc(), DSN = "myDSN", 
                Database = "myDb", 
                Authentication = "ActivedirectoryPassword",
                encoding = "windows-1252",
                AutoTranslate="yes", 
                UID = usr, 
                PWD = pas)

I checked some of the users using crazy passwords with special character like :

"1c0d9a1138a[]bb#$2m9d445654_Ñ#)/f8"

How I should change the encoding that this kind of password can be passed to the connection string via odbc in R?

alipprc avatar Nov 17 '21 16:11 alipprc

I've been generating a connection string myself because I ran into this and other things..

pii_conn <-  DBI::dbConnect(drv = odbc::odbc(),
                            .connection_string = c(
                              "DSN={myDSN}",
                              "Database=myDb",
                              sprintf("UID={%s}", user_and_pass[1]),
                              sprintf("PWD={%s}", user_and_pass[2]) 
                            ) %>% paste(.data, sep=";", collapse="")
)

This has the advantage you can quote or not as you see fit - SQL server likes {} as well, note how I've used them in the example. It also means you can just google odbc connections for your db and use the parameters directly instead of trying to guess how odbc will pass them through.

akdor1154 avatar Dec 14 '21 06:12 akdor1154

@akdor1154 My password contains "," "{" and "$". I tried your approach with connection string, but it didn't help. Connecting to teradata in Python, the same password works as-is when using Python's teradatasql.connect() Any ideas about how could I get things work using odbc in R? Thanks a lot.

daniepi avatar Jan 05 '22 08:01 daniepi

@akdor1154 That method did not works for me as well. I made another test also : I have created a test user in my database and set the authentication to sql server password, so my connection string looks like :

ch <- odbc::dbConnect(odbc(), DSN = "myDSN", 
                Database = "myDb", 
                Authentication = "SqlPassword",
                encoding = "windows-1252",
                AutoTranslate="yes", 
                UID = usr, 
                PWD = pas)

and now everything is fine !!! with the same set of special characters in my password ! So what ever it is, is related to the authentication method part !

alipprc avatar Jan 18 '22 21:01 alipprc

What about other special characters in password when using ODBC? Do you have any other reference?

corinabioinformatic avatar Nov 29 '22 16:11 corinabioinformatic

When I use the configs given in .odbc.ini, are special characters are problem? And if yes, how do I escape them?

LucasSchelkes-BA avatar Jan 10 '23 15:01 LucasSchelkes-BA

Duplicate of #367

hadley avatar Apr 24 '23 15:04 hadley