如何在 Ionic 3 中使用数据库方法(Sqlite 插件)作为提供者?
How to use database method ( Sqlite plugin) as a Provider in Ionic 3?
我在 Ionic 3 App 中使用 SqLite 原生插件。
根据 documentation ,它按预期工作。
在 app.commponent.ts 中,我创建了 table 这样的:
this.sqlite.create({
name: 'sdt.db',
location: 'default'
})
.then((db) => {
//create table if not exists!!!
this.db = db;
console.log(" within app components");
var createTableAccount = "CREATE TABLE IF NOT EXISTS 'accounts' ( 'accountid' INTEGER, 'accountName' TEXT NOT NULL, 'remarks' TEXT, 'initBalance' INTEGER NOT NULL, PRIMARY KEY('accountid') );"
this.db.transaction(function(tx) {
tx.executeSql(createTableAccount);
//todo: create a transaction table .........
//todo: insert data to table
}).then(() => {
console.log("basic structure sql executed")
//this.presentToast();
}).catch(e => console.log(e));;
});
在Home.tspages构造函数中,我是这样使用的
this.sqlite.create({
name: 'sdt.db',
location: 'default'
})
.then((db) => {
this.db = db;
});
页数:
ionViewDidLoad() {
this.loader = this.loading.create({
content: 'Loading ...',
cssClass: "loadingControllerCustomCss"
});
this.loader.present().then(() => {
this.getBalance();
});
}
详细方法是
getBalance() {
var balanceQuery = "select sum(trxamount) sumofamount from transactiontable";
this.db.executeSql(balanceQuery, [])
.then((data) => {
this.balance = data.rows.item(0).sumofamount;
}
)
.catch(e => console.log(e));
}
但我想创建table一次并重用 getBalance() 方法,这样我就不必重复 code.Hence 的片段,我想使用一个提供者(例如 BackendService)作为一种服务方法,它可以对所有人重复使用页数。
最佳做法是什么?
任何人都可以帮助 作为 Ionic 3 中的提供程序的 sqlite 本机插件的完整示例 其中将显示打开数据库、创建模式和获取数据?
提前致谢!
好的,首先您需要安装本机插件,请参阅此处的说明:https://ionicframework.com/docs/native/sqlite/
完成后创建您的 sqlite 提供程序。通常在 src 中你做文件夹 "providers" 并添加 sqlite.ts.
其内容:
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@Injectable()
export class SqLiteProvider {
// we need to declare a var that will point to the initialized db:
public db: SQLiteObject;
constructor(
private sqlite: SQLite
)
{
this.sqlite.create({
name: 'data.db',
location: 'default'
}).then((db: SQLiteObject) => {
// here we will assign created db to our var db (see above)
this.db = db;
// we can now create the table this way:
this.db.executeSql('create table danceMoves(name VARCHAR(32))', {})
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
}).catch(e => console.log(e));
}
// here in this provider we create getBalance method, that should be accessible by other pages:
getBalance() {
// we will do Promise here:
return new Promise((resolve, reject) => {
let balanceQuery = "select sum(trxamount) sumofamount from transactiontable";
this.db.executeSql(balanceQuery, []).then((data) => {
let balance = data.rows.item(0).sumofamount;
// if we successfully obtain data - we resolve it, means it can be available via callback
resolve(balance)
}).catch((err)=>{ console.log(err); reject(err)}) // we deal with errors etc
})
}
}
然后,如果您想在应用程序的任何位置使用它,则需要创建此 "global scope" 提供程序。为此,您转到 app.module.ts 并导入:
从'../../providers/sqlite'导入{SqLiteProvider}; // 确保这是您创建 sqlite.ts
的文件夹
现在对于任何 page/component 如果您需要使用此提供程序,您只需:
- 导入它,
- 然后使用构造函数对其进行初始化。
// 一些页面或组件:
从'../../providers/sqlite'导入{SqLiteProvider}
...
构造函数(
私有 sqlProvider:SqLiteProvider
){}
现在,在此页面中,您可以通过
访问此提供程序的方法
this.sqlProvider.getBalance().then((数据)=>{ console.log(数据)}).
我在 Ionic 3 App 中使用 SqLite 原生插件。 根据 documentation ,它按预期工作。 在 app.commponent.ts 中,我创建了 table 这样的:
this.sqlite.create({
name: 'sdt.db',
location: 'default'
})
.then((db) => {
//create table if not exists!!!
this.db = db;
console.log(" within app components");
var createTableAccount = "CREATE TABLE IF NOT EXISTS 'accounts' ( 'accountid' INTEGER, 'accountName' TEXT NOT NULL, 'remarks' TEXT, 'initBalance' INTEGER NOT NULL, PRIMARY KEY('accountid') );"
this.db.transaction(function(tx) {
tx.executeSql(createTableAccount);
//todo: create a transaction table .........
//todo: insert data to table
}).then(() => {
console.log("basic structure sql executed")
//this.presentToast();
}).catch(e => console.log(e));;
});
在Home.tspages构造函数中,我是这样使用的
this.sqlite.create({
name: 'sdt.db',
location: 'default'
})
.then((db) => {
this.db = db;
});
页数:
ionViewDidLoad() {
this.loader = this.loading.create({
content: 'Loading ...',
cssClass: "loadingControllerCustomCss"
});
this.loader.present().then(() => {
this.getBalance();
});
}
详细方法是
getBalance() {
var balanceQuery = "select sum(trxamount) sumofamount from transactiontable";
this.db.executeSql(balanceQuery, [])
.then((data) => {
this.balance = data.rows.item(0).sumofamount;
}
)
.catch(e => console.log(e));
}
但我想创建table一次并重用 getBalance() 方法,这样我就不必重复 code.Hence 的片段,我想使用一个提供者(例如 BackendService)作为一种服务方法,它可以对所有人重复使用页数。
最佳做法是什么?
任何人都可以帮助 作为 Ionic 3 中的提供程序的 sqlite 本机插件的完整示例 其中将显示打开数据库、创建模式和获取数据?
提前致谢!
好的,首先您需要安装本机插件,请参阅此处的说明:https://ionicframework.com/docs/native/sqlite/
完成后创建您的 sqlite 提供程序。通常在 src 中你做文件夹 "providers" 并添加 sqlite.ts.
其内容:
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@Injectable()
export class SqLiteProvider {
// we need to declare a var that will point to the initialized db:
public db: SQLiteObject;
constructor(
private sqlite: SQLite
)
{
this.sqlite.create({
name: 'data.db',
location: 'default'
}).then((db: SQLiteObject) => {
// here we will assign created db to our var db (see above)
this.db = db;
// we can now create the table this way:
this.db.executeSql('create table danceMoves(name VARCHAR(32))', {})
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
}).catch(e => console.log(e));
}
// here in this provider we create getBalance method, that should be accessible by other pages:
getBalance() {
// we will do Promise here:
return new Promise((resolve, reject) => {
let balanceQuery = "select sum(trxamount) sumofamount from transactiontable";
this.db.executeSql(balanceQuery, []).then((data) => {
let balance = data.rows.item(0).sumofamount;
// if we successfully obtain data - we resolve it, means it can be available via callback
resolve(balance)
}).catch((err)=>{ console.log(err); reject(err)}) // we deal with errors etc
})
}
}
然后,如果您想在应用程序的任何位置使用它,则需要创建此 "global scope" 提供程序。为此,您转到 app.module.ts 并导入:
从'../../providers/sqlite'导入{SqLiteProvider}; // 确保这是您创建 sqlite.ts
的文件夹现在对于任何 page/component 如果您需要使用此提供程序,您只需: - 导入它, - 然后使用构造函数对其进行初始化。
// 一些页面或组件:
从'../../providers/sqlite'导入{SqLiteProvider} ... 构造函数( 私有 sqlProvider:SqLiteProvider ){}
现在,在此页面中,您可以通过
访问此提供程序的方法this.sqlProvider.getBalance().then((数据)=>{ console.log(数据)}).