HI I am using R and want to save the table views in ch but it does not work
I have the dsn file saved on a directory called dsnfiles:
ch <- odbcConnect("S:/dsnfiles/databasex.dsn")
sqlTables(ch)
Please can you help me? This code does not work.
Thanks
A
DSN files are generally stored on the system level, so usually not arbitrary files like that. I believe that the
odbcpackage supports arbitrary files on macos and linux (not windows), but I haven't tested this withRODBC. Having said that, it's not hard to parse that file and get what you need.For my configuration, I have a SQL Server instance running on my local host, port 21433.
I've created a DSN file named
"~/StackOverflow/14549856/somedatabase.dsn"with these contents:(replacing uid/pwd as appropriate).
I'm assuming that since you're just referencing a single file with no other qualifications, that the entry you need (
[ODBC]in my example) is the only entry in the file, so I'll run with that. If you have any malformed entries, empty lines, or comments in that file, you may need to do a little more with the connection string formatting here.So let's convert this file into a "connection string" (here's a good reference for these strings: https://www.connectionstrings.com/).
(The
[-1]gets rid of the[ODBC]header. We use all other lines.)We can use this directly in an opening call:
It's possible (even likely) that your DSN file has more than this example. If there are multiple entries (e.g.,
[ODBC]and[SomeOtherODBC]), then you'll need a bit more work. Below is an example that might work (might not, depending on comments, etc).New DSN file contents: