cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
djhanson
Level V

Open Database() with csv/txt example?

Does anyone have an Open Database() example for ODBC'ing to a .csv or .txt file?  I can get ODBC to work fine with Excel 2007 .xlsx but haven't deduced the text file connection string correctly for opening a .csv file.  I'm even referencing the correct "User DSN" name string via Windows ODBC Data Source Administrator.  So I'm wondering if I have the wrong "DriverId" shown below?  thx... dj

This works:

dt_excel=open database("DSN=Excel Files xlsx;DBQ=C:\Data.xlsx;DriverId=790;MaxBufferSize=2048;PageTimeout=5;","SELECT *","Result");

This doesn't work:

dt_csv=open database("DSN=Text File;DBQ=C:\Data.csv;DriverId=27;MaxBufferSize=2048;PageTimeout=5;","SELECT *","Result");

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Open Database() with csv/txt example?

Have you tried this variant? It worked with a sample file, however I had problems with delimiters. But I at least got a table with (messed up) data from the text file.

dt_csv = Open Database( "DSN=Text File;DBQ=C:\;", "SELECT * FROM Data.csv", "Result" );

View solution in original post

4 REPLIES 4

Re: Open Database() with csv/txt example?

I don't think I have ever referenced "DriverId" in my DSN strings.  Will it work if you leave it out altogether?

djhanson
Level V

Re: Open Database() with csv/txt example?

Thanks Chad.  I tried removing the "DriverId" string and other subsequent strings and get the same error message.  It's as if it cannot find my Text File driver (eventhough it appears fine in the Windows ODBC manager).  Else, I've got some sort of typo/mismatch in my connection string?  Odd, as this whole thing works great in Excel (but my data files are csv's so I'd rather not have to convert them to .xlsx files to use ODBC).

This doesn't work:

dt_csv=open database("DSN=Text File;DBQ=C:\Data.csv;","SELECT *","Result");


Error message that results:

[Microsoft][ODBC Text Driver] '(unknown)' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

ms
Super User (Alumni) ms
Super User (Alumni)

Re: Open Database() with csv/txt example?

Have you tried this variant? It worked with a sample file, however I had problems with delimiters. But I at least got a table with (messed up) data from the text file.

dt_csv = Open Database( "DSN=Text File;DBQ=C:\;", "SELECT * FROM Data.csv", "Result" );

djhanson
Level V

Re: Open Database() with csv/txt example?

Hi MS, yep, late last night I finally figured it out!  Yes, it is exactly what you posted above syntax-wise.  Thanks for replying to this thread.  For the record, here's what I learned about doing an ODBC to a text file.  It really has nothing to do with JSL, but rather the industry convention/syntax required to do ODBC to a text file itself:

a. Create schema.ini - must create this file to support the text file format.  Place it in the same directory as the text file.  Easy to create via Notepad.  Google schema.ini for ODBC text file for details.

b. Use correct ODBC syntax - unlike say Excel ODBC DBQ string, text file ODBC DBQ string does not reference the path+filename but rather just the path.  This is what I got hung up on.  So this is what works (just as you posted above - the key is to reference the DBQ=<path> and not DBQ=<path+filename> and then split up the filename into the SQL FROM statement):

dt_csv=open database("DSN=Text File;DBQ=C:\;","SELECT * FROM Data.csv","Result");

c. Minor detail - just a note.  If you're using Win7, there are two different locations for the ODBC Manager.  One is the 32 bit location and the other is the 64 bit location.  Microsoft named them like Greenland and Iceland (sort of misleading).  Just be aware of this when/if you're adding/modifying say the ODBC Text File driver.  I hear in Win8 that they may make these separate names or identifiers somehow.

Win7 ODBC Managers:

32 bit: C:\Windows\SysWOW64\odbcad32.exe

64 bit: C:\Windows\System32\odbcad32.exe

Anyways, the above a.) + b.) does allow me to use JSL to ODBC to a raw text file.  Very nice... ...dj