需要注意,sqlite只能在应用程序中才可以使用,是不支持各种小程序以及web端的。
sqlite与普通的 sql语句一样。
openDB(){
plus.sqlite.openDatabase({
name:'pop', //数据库名称
path:'_doc/pop.db', //数据库地址,没有文件自动创建
success(){
console.log("打开成功"); //成功回调
},
fail(e){
console.log(e); //失败回调
}
})
}
creatDB(){
plus.sqlite.executeSql({
name:'pop',
//表格创建或者打开,后面为表格结构
sql:'create table if not exists userInfo("list" INTEGER PRIMARY KEY AUTOINCREMENT,"id" TEXT,"name" TEXT,"gender" TEXT,"avatar" TEXT)',
success(res){
console.log("创建成功")
},
fail(e){
console.log(e)
}
})
}
insertDB(){
plus.sqlite.executeSql({
name:'pop',
sql:'insert into userInfo(id,name,gender,avatar) values("'+123+'","'+"老王"+'","'+"1"+'","'+"0"+'")',//正常sql语句
success(){
console.log("新增成功");
},
fail(e){
reject(e);
}
})
},
selectDB(){
plus.sqlite.selectSql({
name:'pop',
sql:"select * from userInfo",//正常sql语句
success(res){
console.log(res);
},
fail(e){
console.log(e);
}
})
},
updateDB(){
plus.sqlite.executeSql({
name:'pop',
sql:"update userInfo set name = '老李' where id = 123",//正常sql语句
success(res){
console.log(res);
},
fail(e){
console.log(e);
}
})
}
deleteDB(){
plus.sqlite.executeSql({
name:'pop',
sql:"delete from userInfo where id = 123 ",//正常sql语句
success(res){
console.log(res);
},
fail(e){
console.log(e);
}
})
}