ionic3使用SQLite数据库

sudo ionic cordova plugin add cordova-sqlite-storage
因为现在ionic已经是ionic5,ionic3使用蓝牙需要指定版本
sudo npm install --save @ionic-native/sqlite@^4.20.0

在app.moudle.ts中引入SQLite

...
import { SQLite } from '@ionic-native/sqlite';
...
providers: [
...
SQLite
]
 

//下面对增删改查进行了封装

import { Injectable } from '@angular/core';

import 'rxjs/add/operator/map';

import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

import { Utils } from './Utils';

 

/*

Generated class for the StorageProvider provider.

 

See https://angular.io/guide/dependency-injection for more info on providers

and Angular DI.

*/

@Injectable()

export class SqliteProvider {

database: SQLiteObject;

constructor(private sqlite: SQLite) {

this.initDb();

}

 

insert(sql,params,callback?: any) {

this.database.executeSql(sql,params).then((data)=>{

if(callback!=null){

callback(1);

}

},(error)=>{

if(callback!=null){

callback(0);

}

console.log('插入错误'+JSON.stringify(error)+",sql:"+sql);

});

}

 

update(sql,params,callback?: any) {

this.database.executeSql(sql,params).then((data)=>{

if(callback!=null){

callback(1);

}

},(error)=>{

if(callback!=null){

callback(0);

}

console.log('更新错误'+JSON.stringify(error)+",sql:"+sql);

});

}

 

//查询数据结果集

query(sql: string,params,callback?: any) {

this.database.executeSql(sql,params).then((res)=>{

var arrList=[];

var len=res.rows.length;

if(len>0){

        for(var i=0;i<len;i++){

         arrList.push(res.rows.item(i));

        }

//console.log('查询脚本:'+sql+',结果:'+JSON.stringify(arrList));

if(callback!=null){

callback(arrList);

}

}else{

if(callback!=null){

callback(arrList);

}

}

},(error)=>{

console.log('查询错误'+JSON.stringify(error)+",sql:"+sql);

if(callback!=null){

callback();

}

});

}

 

//查询一条数据

queryOne(sql: string,params,callback?: any) {

this.database.executeSql(sql,params).then((res)=>{

var len=res.rows.length;

if(len>0){

if(callback!=null){

callback(res.rows.item(0));

}

}else{

if(callback!=null){

callback(null);

}

}

},(error)=>{

console.log('查询错误'+JSON.stringify(error)+",sql:"+sql);

if(callback!=null){

callback(null);

}

});

}

 

//删除表

droptable(tablename: string,callback?: any){

let drop_table_sql='drop table '+tablename;

let tableexist="select count(*) countnum from sqlite_master where type='table' and name = '"+tablename+"'";

this.query(tableexist,[],(result)=>{

if(result!=null&&result.length>0){

var countnum=result[0].countnum;

if(countnum>=1){

console.log('删除表:'+drop_table_sql);

this.database.executeSql(drop_table_sql,[]).then((data)=>{

if(callback!=null){

callback(1);

}

},(error)=>{

console.log('删除表错误:'+drop_table_sql+',错误原因:'+JSON.stringify(error));

if(callback!=null){

callback(null);

}

});

}else{

if(callback!=null){

callback(0);

}

}

}

});

}

 

//删除数据

delete(delSql: string,params,callback?: any){

this.database.executeSql(delSql,params).then((data)=>{

if(callback!=null){

callback(1);

}

},(error)=>{

console.log('删除数据错误:'+delSql+',错误原因:'+JSON.stringify(error));

if(callback!=null){

callback(null);

}

});

}

 

//创建表

createtable(sql: string,tablename: string,callback?: any){

let tableexist="select count(*) countnum from sqlite_master where type='table' and name = '"+tablename+"'";

this.query(tableexist,[],(result)=>{

if(result!=null&&result.length>0){

var countnum=result[0].countnum;

if(countnum<1){

console.log('创建表:'+sql);

this.database.executeSql(sql,[]).then((data)=>{

if(callback!=null){

callback(1);

}

},(error)=>{

console.log('创建表错误:'+JSON.stringify(error));

if(callback!=null){

callback(null);

}

});

}else{

if(callback!=null){

callback(0);

}

}

}

});

}

 

//创建索引

createindex(sql: string,callback?: any){

this.database.executeSql(sql,[]).then((data)=>{

console.log('创建索引:'+sql);

if(callback!=null){

callback(1);

}

},(error)=>{

console.log('创建索引错误:'+JSON.stringify(error));

if(callback!=null){

callback(null);

}

});

}

 

//初始化db

initDb(callback?: any){

this.sqlite.create({

name: 'sqlite.db',

location: 'default'

}).then((db: SQLiteObject) => {

this.database=db;

if(callback!=null){

callback();

}

}).catch(e => {

console.log("catch2"+JSON.stringify(e));

});

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值