Passing parameters using alasql in angularjs

1.3k views Asked by At

js in my angular js project to export a grid to excel, heres my code:

 inventaire.exportMyDataVille = function(data) {

        var city='Safi';
        alasql('SELECT * INTO XLSX("data.xlsx",{headers:true}) FROM ? WHERE secteur='+city+' GROUP BY secteur,agence,serie', [inventaire.myDataSource]);
    };

what im trying to do is to pass a parametre in the alasql but it gives a empty excel file??is there somthing wrong in the request

2

There are 2 answers

2
Phil Cap On

You need to wrap string values in quotes when applying them in SQL clauses.

Based on your previous choice, use double quotes as follows:

alasql('SELECT * INTO XLSX("data.xlsx",{headers:true}) FROM ? WHERE secteur="'+city+'" GROUP BY secteur,agence,serie', [inventaire.myDataSource]);.

Note the change from '+city+' to "'+city+'"

0
José Flávio On

In this case, the alasql function requires the format ( query, [[data], argument, argument, argumentN] )

So it should be:

alasql('... FROM ? WHERE secteur = ? ...', [inventaire.myDataSource, city]);

inventaire.myDataSource must follow the format [{secteur:'aaa'}, {secteur:'bbb'}, {secteur:'ccc'}]