Ionic实战四:中集成Sqlite数据存储记录-爱代码爱编程
1.插件集成
ionic cordova plugin add cordova-sqlite-storage
npm install --save @ionic-native/sqlite
2.项目中引入
在app.module.ts中加入
import { SQLite, SQLiteObject } from ‘@ionic-native/sqlite’;
在providers中加入
SQLite,
3.页面引入
在需要使用sqlite的页面中引入
import { SQLite, SQLiteObject } from ‘@ionic-native/sqlite’;
database: SQLiteObject;
private sqlite: SQLite,
4.数据库初始化
initDataBase() {
var sqlStr = "create table if not exists taskListTable(id integer primary key autoincrement," +
"createUser varchar(64),createTime varchar(64),patientName varchar(64),status varchar(64)," +
"reason varchar(320),description varchar(2000),digest varchar(320),taskId varchar(64))"
this.sqlite.create({
name: 'yqbapp.db',
location: "default"
})
.then((db: SQLiteObject) => {
db.executeSql(sqlStr, [])
.then((d) => console.log('init SQL:' + JSON.stringify(d)))
.catch(e => console.log(e));
this.database = db;
});
}
说明:手机有ROOT权限或者是在模拟器中可以在data/data/项目包名/yqbapp.db中看到初始化的数据库
5.增删改查方法
//增
insertData(taskId, createUser, patientName) {
this.database.executeSql('insert into taskListTable(taskId,createUser,patientName) values(?,?,?)', [taskId, createUser, patientName])
.then((d) => {
console.log("insert d:" + JSON.stringify(d));
})
.catch(e => console.log(e))
}
//改
updateData(taskId, status) {
this.database.executeSql('update taskListTable set status=? where taskId=?', [status, taskId])
.then((d) => {
console.log("update d:" + JSON.stringify(d));
})
.catch(e => console.log(e))
}
//删
deleteData(taskId) {
this.database.executeSql('delete from taskListTable where taskId=?', [taskId])
.then((d) => {
console.log("delete d:" + JSON.stringify(d));
})
.catch(e => console.log(e))
}
//查
queryData() {
this.database.executeSql("select * from taskListTable", []).then((data) => {
var dataCount = data.rows.length;
console.log("select d:" + JSON.stringify(dataCount));
}).catch(e => console.log(e))
}
6.方法引用
//初始化
this.initDataBase();
//新增this.insertData(1, "createUser","patientName");
//更新
this.updateData(1, 1);
//删除
this.deleteData(1);
//查询
this.queryData();
7.模拟器中数据展示
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/qq_16497617/article/details/108649517