Search code examples
cordovaionic-frameworkionic3httpclientreadfile

Ionic 3: Read a file line by line, and loop through the return


I am working on a script that must fetch SQL queries in a .sql file located in ./assets/, to execute those queries on my local DB with Ionic 3. The idea is that I would like to open the file, store every line separately in an array, then loop on that array with a homemade executeSqlQuery() function.

So far, I think I have been able to open the file thanks to httpClient module, but not a lot more... I have been looking at the File module (native), but couldn't get anything done even with a lot of documentation & tests.

Here is what I did, and I don't know how to go from opening './assets/queries.sql' to executeSqlQuery(queries[i]) :

Excerpt of queries.sql :

INSERT INTO `attaquesinf` VALUES ('bandaltchagui','circulaire','Bandal Tchagui','Bandal Chagi','Coup de pied circulaire niveau moyen.',NULL,NULL,NULL);
INSERT INTO `attaquesinf` VALUES ('dolyotchagui','circulaire','Dolyo Tchagui','Doleo Chagi','Coup de pied circulaire niveau haut.',NULL,NULL,NULL);
INSERT INTO `attaquesinf` VALUES ('biteurotchagui','crochet','Biteuro Tchagui','Biteureo Chagi','Coup de pied de l''intérieur vers l''extérieur dans un mouvement de torsion, avec le bol du pied.',NULL,NULL,NULL);
INSERT INTO `attaquesinf` VALUES ('nakkattchagui','crochet','Nakkat Tchagui','Nakka Chagi','Coup de pied crochet, frappe avec le talon.',NULL,NULL,NULL);

Excerpt of ExPage.ts :

import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';

...

export class ExPage {
    infsql: Observable<any>;

    constructor(public httpClient: HttpClient) {
        this.infsql = this.httpClient.get('./asset/queries.sql')
        this.infsql
            .subscribe(data => {
                console.log('my data: ', data);
            // ====> How can I use data ??
            // ====> How can I loop through the content and executeSqlQuery(each line) ?
            },
            error => {
            console.log("Error reading config file " + error.message + ", " + error.code);
            });
      }
  };
...

Thank you in advance for your help !


Solution

  • I managed to find the solution after ...... some hours of testing. I finally opted for Http.get method that does the jobs.

    In ExPages.ts :

    import { Http } from '@angular/http';
    import 'rxjs/add/operator/map';
    ...
    
    export class ExPage {
    
        requests: Array<{string}>;     //the final array that will contain each line of .sql file
        testtext: string;              //a variable we'll use to test the content of the request
        ...
    
    
        constructor(public http: Http) {
    
            this.http.get('../assets/queries.sql')               //gets the file
                     .map(res => res.text())                      //maps it as text
                     .subscribe(data => {                         //saves the data in our array
                         this.requests = data.split('\n');
                         this.testtext = this.requests[20];
                     })
        }
    }
    

    In ExPages.html :

    <p>20th SQL query : {{ testtext }}</p>
    

    Will display the 21st line of my assets/queries.sql file, as the index of our request array starts at 0.

    Thanks for your help @millimoose :)