新建一個(gè)weqsl.js文件? mian.js中添加import Websqldbfrom './assets/js/websql' 耻姥;Vue.use(Websqldb)
關(guān)于wesql有一個(gè)蛋疼的地方就是,你在vue頁(yè)面中無(wú)法獲得weqsl中定義操作數(shù)據(jù)庫(kù)方法return的數(shù)據(jù)
當(dāng)然 node.js中也是一樣,望忆,所以鳄炉,,只能用promise中reslove操作了烟勋,至少本人這樣認(rèn)為滴
main.js中如下
export default {
install(Vue){
var dbname='websql';/*數(shù)據(jù)庫(kù)名*/
? ? var version ='1.0'; /*數(shù)據(jù)庫(kù)版本*/
? ? var dbdesc ='websql'; /*數(shù)據(jù)庫(kù)描述*/
? ? var dbsize =2*1024*1024; /*數(shù)據(jù)庫(kù)大小*/
? ? var dataBase =null; /*暫存數(shù)據(jù)庫(kù)對(duì)象*/
/*數(shù)據(jù)庫(kù)中的表單名*/
? ? var websqlTable ="websqlTable";
? ? //drop table friend 刪除表
//打開(kāi)數(shù)據(jù)庫(kù)
? ? dataBase = window.openDatabase(dbname, version, dbdesc, dbsize,function() {});
}
Vue.prototype.websqlOpenDB=function () {
console.log("數(shù)據(jù)庫(kù)打開(kāi)成功")
return dataBase;
}
//
這里添加操作數(shù)據(jù)庫(kù)的方法
}
關(guān)于操作websql數(shù)據(jù)庫(kù)规求,嗯筐付,,時(shí)間久遠(yuǎn)阻肿,不知道理解的對(duì)不對(duì)家妆,那就在我的代碼里復(fù)制吧
//新建firend表
Vue.prototype.frienddbbcreat=function () {
var tableName="friend"+localStorage.getItem("userid")
var creatTableSQL ='CREATE TABLE IF? NOT EXISTS '+ tableName +' (userid number,text text,time text,type text,photo text,name text,PRIMARY KEY (`userid`))';
? dataBase.transaction(function (ctx,result) {
ctx.executeSql(creatTableSQL,[],function(ctx,result){
console.log("表創(chuàng)建成功 " + tableName);
? ? },function(tx, error){
console.log('創(chuàng)建表失敗:' + tableName + error.message);
? ? });
? });
};
//friend表插入數(shù)據(jù)
Vue.prototype.frienddbinsert=function (obj) {
var tableName="friend"+localStorage.getItem("userid");
? var insterTableSQL ='INSERT INTO ' + tableName +' (userid,text,time,type,photo,name) VALUES (?,?,?,?,?,?)';
? dataBase.transaction(function (ctx) {
ctx.executeSql(insterTableSQL,[obj.userid,obj.text,obj.time,obj.type,obj.photo,obj.name],function (ctx,result){
console.log("插入" + tableName? +"成功");
? ? ? },
? ? ? function (tx, error) {
console.log('插入'+ tableName? +'失敗: ' + error.message);
? ? ? });
? });
};
//friend表更新數(shù)據(jù)1
Vue.prototype.frienddbupdate=function (obj) {
var tableName="friend"+localStorage.getItem("userid")
var sql ='UPDATE ' + tableName +' SET text = ? ,time = ?, type = ?? WHERE userid = ?';
? dataBase.transaction(function (ctx,result) {
ctx.executeSql(sql,[obj.text,obj.time,obj.type,obj.userid],function(ctx,result){
console.log("更新成功 " + tableName + name);
? ? },function(tx, error){
console.log('更新失敗:' + tableName? + name + error.message);
? ? });
? });
};
//通過(guò)userid從chat表獲取數(shù)據(jù)
Vue.prototype.chatget=function (userid) {
var tableName="chat"+localStorage.getItem("userid");
? var sql ='SELECT * FROM ' + tableName +' WHERE send = ?? OR? receive = ? order by time asc'
? var pp =new Promise(function (resolve,reject) {
dataBase.transaction(function (ctx) {
ctx.executeSql(sql,[userid,userid],function (ctx,result){
resolve(result)
},
? ? ? ? function (tx, error) {
console.log('chatget查詢失敗: ' + error.message);
? ? ? ? ? reject(error)
});
? ? });
? });
? return pp
}