-
Notifications
You must be signed in to change notification settings - Fork 664
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) })
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);
});
alasql.promise('SELECT * INTO CSV("my.csv", {headers:false}) FROM ?',[data])
.then(function(){
console.log('Data saved');
}).catch(function(err){
console.log('Error:', err);
});;
For CSV you can set the following options
-
header is default
true
. If set tofalse
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 defaultfalse
. Iftrue
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.
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo