0% found this document useful (0 votes)
2 views2 pages

Tutorial SQL Com CSV

The document provides a tutorial on using CSV (Comma Separated Values) support within a database, detailing functions like CSVREAD and CSVWRITE for reading and writing CSV files. It explains how to import data from CSV files into database tables and how to use CSV tools in Java applications for both reading and writing CSV files. Performance considerations are also mentioned, advising against using CSVREAD in joins and suggesting data import into temporary tables instead.

Uploaded by

Rui Guilherme
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Tutorial SQL Com CSV

The document provides a tutorial on using CSV (Comma Separated Values) support within a database, detailing functions like CSVREAD and CSVWRITE for reading and writing CSV files. It explains how to import data from CSV files into database tables and how to use CSV tools in Java applications for both reading and writing CSV files. Performance considerations are also mentioned, advising against using CSVREAD in joins and suggesting data import into temporary tables instead.

Uploaded by

Rui Guilherme
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

14/11/2014 Tutorial

CSV (Comma Separated Values) Support


The CSV file support can be used inside the database using the functions CSVREAD and CSVWRITE , or it
can be used outside the database as a standalone tool.

Reading a CSV File from Within a Database


A CSV file can be read using the function CSVREAD . Example:

SELECT * FROM CSVREAD('[Link]');

Please note for performance reason, CSVREAD should not be used inside a join. Instead, import the data first
(possibly into a temporary table), create the required indexes if necessary, and then query this table.

Importing Data from a CSV File


A fast way to load or import data (sometimes called 'bulk load') from a CSV file is to combine table creation with
import. Optionally, the column names and data types can be set when creating the table. Another option is to
use INSERT INTO ... SELECT .

CREATE TABLE TEST AS SELECT * FROM CSVREAD('[Link]');


CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))
AS SELECT * FROM CSVREAD('[Link]');

Writing a CSV File from Within a Database


The built-in function CSVWRITE can be used to create a CSV file from a query. Example:

CREATE TABLE TEST(ID INT, NAME VARCHAR);


INSERT INTO TEST VALUES(1, 'Hello'), (2, 'World');
CALL CSVWRITE('[Link]', 'SELECT * FROM TEST');

Writing a CSV File from a Java Application


The Csv tool can be used in a Java application even when not using a database at all. Example:

import [Link].*;
import [Link];
import [Link];
public class TestCsv {
public static void main(String[] args) throws Exception {
SimpleResultSet rs = new SimpleResultSet();
[Link]("NAME", [Link], 255, 0);
[Link]("EMAIL", [Link], 255, 0);
[Link]("Bob Meier", "[Link]@[Link]");
[Link]("John Jones", "[Link]@[Link]");
new Csv().write("data/[Link]", rs, null);
}
}

Reading a CSV File from a Java Application


It is possible to read a CSV file without opening a database. Example:

import [Link].*;

[Link] 1/2
14/11/2014 Tutorial

import [Link];
public class TestCsv {
public static void main(String[] args) throws Exception {
ResultSet rs = new Csv().read("data/[Link]", null, null);
ResultSetMetaData meta = [Link]();
while ([Link]()) {
for (int i = 0; i < [Link](); i++) {
[Link](
[Link](i + 1) + ": " +
[Link](i + 1));
}
[Link]();
}
[Link]();
}
}

[Link] 2/2

You might also like