Skip to content

Latest commit

 

History

History
71 lines (55 loc) · 3.03 KB

CSV.md

File metadata and controls

71 lines (55 loc) · 3.03 KB

Keyword CSV

AlaSQL can import from and export to CSV (comma-separated values) format.

Syntax:

    SELECT column INTO CSV(filename,options) FROM tableid;
    SELECT column FROM CSV(filename,options) ;

Please note that when interacting with files AlaSQL will run async. We strongly recommend you to use the promise notation instead of the simple notation: alasql(sql, params, function(data) { console.log(data) })

Import CSV data

    alasql.promise('SELECT * FROM CSV("my.csv", {headers:false})')
            .then(function(data){
                 console.log(data);
            }).catch(function(err){
                 console.log('Error:', err);
            });

You can try this example in jsFiddle

You can also give the full content of a CSV file as a string instead of the path.

You can specify delimiters and quote characters:

    alasql.promise('SELECT * FROM CSV("my.csv", {headers:false, quote:"\'",separator:","})')
            .then(function(data){
                 console.log(data);
            }).catch(function(err){
                 console.log('Error:', err);
            });

Example on how to change the seperator only

   alasql.promise('SELECT * FROM CSV("a.csv",{separator:";"})')
            .then(function(data){
                 console.log(data);
            }).catch(function(err){
                 console.log('Error:', err);
            });

Export to CSV data

    alasql.promise('SELECT * INTO CSV("my.csv", {headers:false}) FROM ?',[data])
            .then(function(){
                 console.log('Data saved');
            }).catch(function(err){
                 console.log('Error:', err);
            });;

Options

For CSV you can set the following options

  • header is default true. If set to false the first row in the CSV file will contain data instead of the column names
  • utf8Bom default depends on header. If headers will be included it will be default true. If headers not included it will be default false. If true BOM will be added to the CSV file (useful in some cases with excel)
  • separator is default ; and can be set to any string used as a seperator
  • quote is default " and can be set to any string used to quote strings
  • raw is default false and can be set to true if you want all data returned as the raw string (so no conversion of numbers)

Please note that default seperator is ; and not , as specified by the RFC. The reason is that Excel cant always handle ','. As Excel usage is such a big use case for the library we rather bother people who can't handle the incorrect .csv file with adding the parameter {seperator:','} than getting questions from all the people who cant make the lib "work" with Excel.

See also: TAB, TSV, XLSX, JSON