如何使用typescript从SQLite DB返回列的和?
下面是我的database.service.ts文件:如何使用typescript从SQLite DB返回列的和?,typescript,sqlite,ionic-framework,Typescript,Sqlite,Ionic Framework,下面是我的database.service.ts文件: import { Injectable } from '@angular/core'; import { SQLite, SQLiteObject } from "@ionic-native/sqlite/ngx"; import { SQLitePorter } from "@ionic-native/sqlite-porter/ngx"; import { BehaviorSubject, Obs
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from "@ionic-native/sqlite/ngx";
import { SQLitePorter } from "@ionic-native/sqlite-porter/ngx";
import { BehaviorSubject, Observable } from 'rxjs';
import { Platform } from '@ionic/angular';
import { HttpClient } from '@angular/common/http';
export interface scoVar {
id: number,
cLevel: number;
levelScore: number,
totalLevelScore: number,
lStatus: string
}
@Injectable({
providedIn: 'root'
})
export class DatabaseService {
private database: SQLiteObject;
private dbReady: BehaviorSubject<boolean> = new BehaviorSubject(false);
scoInfo = new BehaviorSubject([]);
constructor(private plt: Platform, private sqlitePorter: SQLitePorter, private sqlite: SQLite, private http: HttpClient) {
this.plt.ready().then(() => {
this.sqlite.create({
name: 'scoInfo.db',
location: 'default'
})
.then((db: SQLiteObject) => {
this.database = db;
this.seedDatabase();
});
});
}
seedDatabase() {
this.http.get('assets/seed.sql', { responseType: 'text' })
.subscribe(sql => {
this.sqlitePorter.importSqlToDb(this.database, sql)
.then(_ => {
this.loadScoreTable();
this.dbReady.next(true);
})
.catch(e => console.error(e));
});
}
loadScoreTable() {
return this.database.executeSql('SELECT * FROM scores', []).then(data => {
let scoInfo: scoVar[] = [];
if (data.rows.length > 0) {
for (var i = 0; i < data.rows.length; i++) {
scoInfo.push({
id: data.rows.item(i).id,
cLevel: data.rows.item(i).cLevel,
levelScore: data.rows.item(i).levelScore,
totalLevelScore: data.rows.item(i).totalLevelScore,
lStatus: data.rows.item(i).lStatus
});
}
}
this.scoInfo.next(scoInfo);
});
}
getDatabaseState() {
return this.dbReady.asObservable();
}
getScos(): Observable<scoVar[]> {
return this.scoInfo.asObservable();
}
getSum() {
return this.database.executeSql('SELECT sum(levelScore) FROM scores').then(data => {
return data;
}).catch((e)=>{
return "error on getting sum" + JSON.stringify(e);
});
}
}
下面是我的.html内容
<ion-header>
<ion-toolbar color="primary">
<ion-title>Dzo for Kids App</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div>
<div>
<ion-list *ngFor="let s of scoInfo">
<ion-item>
<h2>{{ s.id }}. {{ s.levelScore }}</h2>
<p>{{ s.cLevel }}</p>
<p>{{ s.totalLevelScore }}</p>
<p>{{ s.lStatus }}</p>
</ion-item>
</ion-list>
<ion-button expand="block" (click)="getLevelStatus()">Get Data</ion-button>
</div>
</div>
<div>
{{sum}}
</div>
</ion-content>
根据我用来返回sum值的sql命令,“sum”的值应该是“14”。我没有得到[目标承诺]是什么,我为什么得到它?为什么我得不到总值?下面是我在sqlite db中的表格
0: {id: 1, cLevel: 1, levelScore: 5, totalLevelScore: 10, lStatus: "N/A"}
1: {id: 2, cLevel: 2, levelScore: 3, totalLevelScore: 10, lStatus: "N/A"}
2: {id: 3, cLevel: 3, levelScore: 2, totalLevelScore: 10, lStatus: "N/A"}
3: {id: 4, cLevel: 4, levelScore: 3, totalLevelScore: 10, lStatus: "N/A"}
4: {id: 5, cLevel: 5, levelScore: 1, totalLevelScore: 10, lStatus: "N/A"}
有人能帮我吗?我急需帮助。带有Order By子句的SQLite SUM()函数
从tablename[其中条件]中选择SUM(表达式)
[表达分组]
> SELECT d.dept_name,SUM(e.salary) FROM emp_master e, dept_master d
> WHERE e.dept_id=d.dept_id GROUP BY e.dept_id;
0: {id: 1, cLevel: 1, levelScore: 5, totalLevelScore: 10, lStatus: "N/A"}
1: {id: 2, cLevel: 2, levelScore: 3, totalLevelScore: 10, lStatus: "N/A"}
2: {id: 3, cLevel: 3, levelScore: 2, totalLevelScore: 10, lStatus: "N/A"}
3: {id: 4, cLevel: 4, levelScore: 3, totalLevelScore: 10, lStatus: "N/A"}
4: {id: 5, cLevel: 5, levelScore: 1, totalLevelScore: 10, lStatus: "N/A"}
> SELECT d.dept_name,SUM(e.salary) FROM emp_master e, dept_master d
> WHERE e.dept_id=d.dept_id GROUP BY e.dept_id;