A simple code how to read/import a CSV file into an array (dyn_dyn_string).
main()
{
string content;
fileToString(DATA_PATH+"/csvtest.txt", content); // read file into a string variable
dyn_dyn_string result;
dyn_string lines = strsplit(content, "\n"); // split the string into lines
for ( int i=1; i<=dynlen(lines); i++ ) {
result[i]=strsplit(lines[i], ";"); // delimiter ";"
}
DebugTN(result);
}
Limitations:
* Don’t do it in that way with very big files because it is read from the file into memory
* The delimiter “;” cannot be inside of a cell value
With oracle you can use external tables to access CSV files. After creating an external table it is possible to access the file by SQL:
select * from ext_tab
CREATE DIRECTORY EXT_DIR AS '/home/oracle/data';
-- Create table
create table ext_tab
(
Col1 VARCHAR2(30),
Col2 VARCHAR2(30),
Col3 VARCHAR2(30)
)
organization external
(
type ORACLE_LOADER
default directory EXT_DIR
access parameters
(
RECORDS DELIMITED BY NEWLINE FIELDS TERMINATED BY ";"
)
location (EXT_DIR:csvtest.csv')
)
reject limit UNLIMITED;