*.db文件放在 Hbuilder 项目目录里面 运行 或者 生成本地打包资源 在APP中会出现读写问题,
解决办法可以参考:
https://ask.dcloud.net.cn/question/129044
也可以
openDatabase 方法调用的时候会自动在_UNI__****/doc/ 目录创建一个 .db文件 该.db里面是空的
然后判断当前数据库是否存在相关表,如果存在执行查询、插入
如果不存在 需要将创建表的相关脚本都执行一遍
代码:
methods: {
openSqlite: function(success, error){
//打开本地数据库
var isOpen = plus.sqlite.isOpenDatabase({
name:'history',
path:'_doc/history.db',
})
if(!isOpen){
plus.sqlite.openDatabase({
name:'history',
path:'_doc/history.db',
success:function(e){
console.log(e);
success(e);
},
fail:function(e){
console.log(e);
error(e);
}
});
}else{
success();
}
},
querySqlite: function(sql, success, error){
plus.sqlite.selectSql({
name:'history',
sql:sql,
success:function(e){
console.log(e);
success(e);
},
fail:function(e){
console.log(e);
error(e);
}
})
},
executeSqlite: function(sql, success, error){
plus.sqlite.executeSql({
name:'history',
sql:sql,
success:function(e){
console.log(e);
success(e);
},
fail:function(e){
console.log(e);
error(e);
}
});
},
closeSqlite: function(){
plus.sqlite.closeDatabase({
name:'history',
success:function(e){
console.log(e);
uni.showToast({
icon: 'none',
title: '关闭成功!' JSON.stringify(e),
})
},
fail:function(e){
console.log(e);
uni.showToast({
icon: 'none',
title: '关闭失败!' JSON.stringify(e),
})
}
});
},
submit:function(){
var that = this;
if(true){
that.openSqlite(function(){
that.querySqlite("select * from sqlite_master where name = 'scan_m' and type='table'", function(e){
if(e.length == 0){
console.log('创建数据表!');
that.executeSqlite("CREATE TABLE scan_m(id int primary key not null, loginId text not null, gzdd text not null, scanTime text not null, labelList text not null)", function(e){
console.log('创建数据表成功!');
that.querySqlite('select max(id) as maxId from scan_m', function(e){
var id = 1,
loginId = "",
gzdd = "",
rq = "",
data = "";
if(e.length == 0){
console.log('数据不存在!');
id = 1
}else{
console.log('数据存在!');
id = e[0]['maxId'] 1;
}
loginId = getApp().globalData.loginId;
gzdd = '123';//that.gzddCalc;
rq = (new Date()).toISOString();
data = 'adsfad';//labelListStr;
that.executeSqlite("insert into scan_m (id, loginId, gzdd, scanTime, labelList) values (" id ",'" loginId "','" gzdd "','" rq "','" data "')",function(){
console.log('插入数据成功!');
that.closeSqlite();
},function(e){
console.log('插入数据失败!' JSON.stringify(e));
that.closeSqlite();
});
},function(e){
that.closeSqlite();
});
},function(e){
console.log('创建数据表失败!');
that.closeSqlite();
})
}else{
console.log('数据表存在!');
that.querySqlite('select max(id) as maxId from scan_m', function(e){
var id = 1,
loginId = "",
gzdd = "",
rq = "",
data = "";
if(e.length == 0){
console.log('数据不存在!');
id = 1
}else{
console.log('数据存在!');
id = e[0]['maxId'] 1;
}
loginId = getApp().globalData.loginId;
gzdd = '123';//that.gzddCalc;
rq = (new Date()).toISOString();
data = 'adsfad';//labelListStr;
that.executeSqlite("insert into scan_m (id, loginId, gzdd, scanTime, labelList) values (" id ",'" loginId "','" gzdd "','" rq "','" data "')",function(){
console.log('插入数据成功!');
that.closeSqlite();
},function(e){
console.log('插入数据失败!' JSON.stringify(e));
that.closeSqlite();
});
},function(e){
that.closeSqlite();
});
}
}, function(e){
that.closeSqlite();
});
});
return false;
}
}
}