【问题标题】:Ionic 3: Read a file line by line, and loop through the returnIonic 3:逐行读取文件,并循环返回
【发布时间】:2018-10-24 01:24:35
【问题描述】:

我正在编写一个脚本,该脚本必须在位于 ./assets/ 的 .sql 文件中获取 SQL 查询,以便使用 Ionic 3 在我的本地数据库上执行这些查询。我想打开该文件,将每一行分别存储在一个数组中,然后使用自制的 executeSqlQuery() 函数在该数组上循环。

到目前为止,我认为由于 httpClient 模块,我已经能够打开文件,但没有更多...我一直在查看 File 模块(本机),但即使使用了也无法完成任何操作大量的文档和测试。

这是我所做的,我不知道如何从打开 './assets/queries.sql' 到 executeSqlQuery(queries[i]) :

querys.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);

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);
            });
      }
  };
...

提前感谢您的帮助!

【问题讨论】:

  • data() 的值是多少?也许只是 data.split('\n') 就足够了,但我不知道 Ionic 的 HttpClient 是如何工作的。
  • 如何探索数据中的内容?我试图在我的视图中将 {{ data }} 放在

    之间,但我没有在屏幕上显示任何内容......我会在找到什么后立即尝试 data.split(\n)在里面以及如何使用它,谢谢! :)
  • console.log
  • 在哪里可以阅读控制台日志?

标签: cordova ionic-framework ionic3 httpclient readfile


【解决方案1】:

经过......几个小时的测试,我设法找到了解决方案。我最终选择了 Http.get 方法来完成这项工作。

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];
                 })
    }
}

ExPages.html

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

将显示我的 assets/queries.sql 文件的第 21 行,因为我们的 request 数组的索引从 0 开始。

感谢您的帮助@millimoose :)

【讨论】:

猜你喜欢
  • 2012-11-12
  • 2012-01-24
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多