Ionic 3:逐行读取文件,并循环遍历 return
Ionic 3: Read a file line by line, and loop through the return
我正在编写一个脚本,该脚本必须在位于 ./assets/ 的 .sql
文件中获取 SQL 查询,以便使用 Ionic 3 在我的本地数据库上执行这些查询。这个想法是我想打开文件,将每一行分别存储在一个数组中,然后使用自制的 executeSqlQuery() 函数在该数组上循环。
到目前为止,我想我已经能够通过 httpClient 模块打开文件,但没有更多...我一直在查看 File 模块(本机),但无法完成任何事情即使有很多文档和测试。
这是我所做的,我不知道如何从打开 './assets/queries.sql' 到 executeSqlQuery(queries[i]) :
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);
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);
});
}
};
...
提前感谢您的帮助!
经过……几个小时的测试,我设法找到了解决方案。我最终选择了完成工作的 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 :)
我正在编写一个脚本,该脚本必须在位于 ./assets/ 的 .sql
文件中获取 SQL 查询,以便使用 Ionic 3 在我的本地数据库上执行这些查询。这个想法是我想打开文件,将每一行分别存储在一个数组中,然后使用自制的 executeSqlQuery() 函数在该数组上循环。
到目前为止,我想我已经能够通过 httpClient 模块打开文件,但没有更多...我一直在查看 File 模块(本机),但无法完成任何事情即使有很多文档和测试。
这是我所做的,我不知道如何从打开 './assets/queries.sql' 到 executeSqlQuery(queries[i]) :
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);
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);
});
}
};
...
提前感谢您的帮助!
经过……几个小时的测试,我设法找到了解决方案。我最终选择了完成工作的 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 :)