websql操作类封装
Posted 我想我会成功
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了websql操作类封装相关的知识,希望对你有一定的参考价值。
在之前,我写了一个websql的封装类库,代码如下:
上述代码存在的问题非常明显,由于websql操作都是异步操作,当我们为了获取到websql操作的结果之后再进行后续操作时,往往是通过回调函数来实现的,当回调一多的时候,回调地狱就出现了,为了解决回调地狱问题,我将通过Promise来改写,后续调用时,可以直接通过await和async来调用,或者直接通过Promise链式调用也是可以的。
现在我将通过ES6的语法重写之前的封装类,为了应用ES6中js面向对象的思想,我这里用到了class,最终代码如下:
import { browserVersions } from ‘@/utils/utils.js‘; class SmpWebSql { constructor(options) { options = options || {}; this.database = null; this.DateBaseName = options.DateBaseName || ‘RedDB‘; this.Version = options.Version || ‘1.0‘; this.Description = options.Description || ‘智维离线工单数据库‘; this.DataBaseSize = options.DataBaseSize || 2 * 1024 * 1024; this.init(); } /** * 初始化数据库 */ init() { this.database = openDatabase( this.DateBaseName, this.Version, this.Description, this.DataBaseSize ); } /** * 批量添加字段 * @param {*} tableName 表名 * @param {*} arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} index BLOG字段所在的索引位置 * @param {*} isFirst 是否是第一次创建表 */ addBlob(tableName, arr, index, isFirst = true) { var _db = this.database; var _me = this; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ‘‘; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } keyC[0] = keyC[0] + ‘ unique‘; _key = keyI.join(‘,‘); index = index == undefined ? 0 : index; keyC[index] = keyC[index] + ‘ BLOB‘; // eslint-disable-next-line promise/param-names _db.transaction(function(tx, result) { // var csql = ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(",") + ‘)‘; // console.log(‘csql:‘ + csql); if (isFirst == true) { tx.executeSql( ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(‘,‘) + ‘)‘ ); } // var sql = ""; for (var s = 0, _len = arr.length; s < _len; s++) { var _value = _me.split(arr[s]); // sql += ‘INSERT INTO ‘ + tableName + ‘ (‘ + _key + ‘) VALUES (‘ + _value + ‘)‘; // console.log("sql:" + sql); tx.executeSql( ‘INSERT INTO ‘ + tableName + ‘ (‘ + _key + ‘) VALUES (‘ + _value + ‘)‘, [], function(tx, result) { resovle(result.rowsAffected); console.log(‘添加成功‘ + result.rowsAffected); }, function(tx) { console.error(‘添加失败‘); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); } _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log(‘error :‘, error); }); } /** * 批量添加字段 * @param {*} tableName 表名 * @param {*} arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} index BLOG字段所在的索引位置 * @param {*} firstKey firstKey 第一个字段是否是主键(默认是) * @param {*} isFirst 是否是第一次创建表 */ patchAddBlob(tableName, arr, index, firstKey = true, isFirst = true) { var _db = this.database; var _me = this; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ‘‘; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } if (firstKey) { keyC[0] = keyC[0] + ‘ unique‘; } _key = keyI.join(‘,‘); index = index == undefined ? 0 : index; keyC[index] = keyC[index] + ‘ text‘; // eslint-disable-next-line promise/param-names _db.transaction(function(tx, result) { // var csql = ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(",") + ‘)‘; // console.log(‘csql:‘ + csql); if (isFirst == true) { tx.executeSql( ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(‘,‘) + ‘)‘ ); } var sql = ‘‘; var _values = []; for (var s = 0, _len = arr.length; s < _len; s++) { _values.push(‘(‘ + _me.split(arr[s]) + ‘)‘); } sql = ‘INSERT INTO ‘ + tableName + ‘ (‘ + _key + ‘) VALUES ‘ + _values.join(‘,‘); // console.log(‘sql:‘ + sql); tx.executeSql( sql, [], function(tx, result) { resovle(result.rowsAffected); // console.log(‘添加成功‘+result.rowsAffected); }, function(tx, error) { console.error(‘添加失败‘, tx, error); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log(‘error :‘, error); }); } /** * 批量添加字段 注 : 数据里面的第一个key 为主键 * @param {*} tableName 表名 * @param {*} arr arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} firstKey firstKey 第一个字段是否是主键(默认是) */ add(tableName, arr, firstKey = true) { var _me = this; var _db = this.database; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ‘‘; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } if (firstKey) { keyC[0] = keyC[0] + ‘ unique‘; } _key = keyI.join(‘,‘); _db.transaction(function(tx) { // /var csql = ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(",") + ‘)‘; // console.log(‘csql:‘ + csql); tx.executeSql( ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(‘,‘) + ‘)‘ ); // var sql = ""; for (var s = 0, _len = arr.length; s < _len; s++) { var _value = _me.split(arr[s]); // sql += ‘INSERT INTO ‘ + tableName + ‘ (‘ + _key + ‘) VALUES (‘ + _value + ‘)‘; // console.log("sql:" + sql); tx.executeSql( ‘INSERT INTO ‘ + tableName + ‘ (‘ + _key + ‘) VALUES (‘ + _value + ‘)‘, [], function(tx, result) { resovle(result.rowsAffected); // console.log(‘添加成功‘+result.rowsAffected); }, function(tx, error) { console.error(‘添加失败‘, error); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); } _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log(‘error :‘, error); }); } /** * 批量添加行记录 注 : 数据里面的第一个key 为主键 * @param {*} tableName 表名 * @param {*} arr arr 更新的数据 [{key1:value1 , key2 : value2 ...},{key1:value1 , key2 : value2 ...}] * @param {*} firstKey firstKey 第一个字段是否是主键(默认是) */ patchAdd(tableName, arr, firstKey = true) { var _me = this; var _db = this.database; // eslint-disable-next-line promise/param-names return new Promise(function(resovle, reject) { if (arr == null) { return this; } var keyC = []; var keyI = []; var _key = ‘‘; arr = arr || []; if (arr && arr.constructor == Array) { for (var i in arr[0]) { keyC.push(i); keyI.push(i); } if (firstKey) { keyC[0] = keyC[0] + ‘ unique‘; } _key = keyI.join(‘,‘); _db.transaction(function(tx) { // /var csql = ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(",") + ‘)‘; // console.log(‘csql:‘ + csql); tx.executeSql( ‘CREATE TABLE IF NOT EXISTS ‘ + tableName + ‘ (‘ + keyC.join(‘,‘) + ‘)‘ ); var sql = ‘‘; var _values = []; for (var s = 0, _len = arr.length; s < _len; s++) { _values.push(‘(‘ + _me.split(arr[s]) + ‘)‘); } sql = ‘INSERT INTO ‘ + tableName + ‘ (‘ + _key + ‘) VALUES ‘ + _values.join(‘,‘); console.log(‘sql:‘ + sql); tx.executeSql( sql, [], function(tx, result) { resovle(result.rowsAffected); // console.log(‘添加成功‘+result.rowsAffected); }, function(tx, error) { console.error(‘添加失败‘); // eslint-disable-next-line prefer-promise-reject-errors reject(false); } ); _key = keyI = keyC = null; resovle(arr.length); }); } }).catch(error => { console.log(‘error :‘, error); }); }
http://view.szonline.net/yule/26683.html
http://view.szonline.net/gundong/20200530/26682.html
http://view.szonline.net/qiwen/20200530/26681.html
http://view.szonline.net/qiwen/20200530/26680.html
http://view.szonline.net/qiwen/20200530/26679.html
http://view.szonline.net/yule/26678.html
http://view.szonline.net/gundong/20200530/26677.html
http://view.szonline.net/qiwen/20200530/26676.html
http://view.szonline.net/qiwen/20200530/26675.html
http://view.szonline.net/qiwen/20200530/26674.html
http://view.szonline.net/yule/26673.html
http://view.szonline.net/gundong/20200530/26672.html
http://view.szonline.net/qiwen/20200530/26671.html
http://view.szonline.net/qiwen/20200530/26670.html
http://view.szonline.net/qiwen/20200530/26669.html
http://view.szonline.net/yule/26668.html
http://view.szonline.net/gundong/20200530/26667.html
http://view.szonline.net/qiwen/20200530/26666.html
http://view.szonline.net/qiwen/20200530/26665.html
http://view.szonline.net/qiwen/20200530/26664.html
http://view.szonline.net/yule/26662.html
http://view.szonline.net/gundong/20200530/26661.html
http://view.szonline.net/qiwen/20200530/26659.html
http://view.szonline.net/qiwen/20200530/26656.html
http://view.szonline.net/qiwen/20200530/26654.html
http://view.szonline.net/yule/26652.html
http://view.szonline.net/gundong/20200530/26651.html
http://view.szonline.net/qiwen/20200530/26650.html
http://view.szonline.net/qiwen/20200530/26649.html
http://view.szonline.net/qiwen/20200530/26648.html
http://view.szonline.net/yule/26647.html
http://view.szonline.net/gundong/20200530/26646.html
http://view.szonline.net/qiwen/20200530/26645.html
http://view.szonline.net/qiwen/20200530/26644.html
http://view.szonline.net/qiwen/20200530/26643.html
http://view.szonline.net/yule/26642.html
http://view.szonline.net/gundong/20200530/26641.html
http://view.szonline.net/qiwen/20200530/26640.html
http://view.szonline.net/qiwen/20200530/26639.html
http://view.szonline.net/qiwen/20200530/26638.html
http://view.szonline.net/yule/26637.html
http://view.szonline.net/gundong/20200530/26636.html
http://view.szonline.net/qiwen/20200530/26635.html
http://view.szonline.net/qiwen/20200530/26634.html
http://view.szonline.net/qiwen/20200530/26633.html
http://view.szonline.net/yule/26632.html
http://view.szonline.net/gundong/20200530/26631.html
http://view.szonline.net/qiwen/20200530/26630.html
http://view.szonline.net/qiwen/20200530/26629.html
http://view.szonline.net/qiwen/20200530/26628.html
http://view.szonline.net/yule/26627.html
http://view.szonline.net/gundong/20200530/26626.html
http://view.szonline.net/qiwen/20200530/26625.html
http://view.szonline.net/qiwen/20200530/26624.html
http://view.szonline.net/qiwen/20200530/26623.html
http://view.szonline.net/yule/26622.html
http://view.szonline.net/gundong/20200530/26621.html
http://view.szonline.net/qiwen/20200530/26620.html
http://view.szonline.net/qiwen/20200530/26618.html
http://view.szonline.net/qiwen/20200530/26617.html
http://view.szonline.net/yule/26616.html
http://view.szonline.net/gundong/20200530/26615.html
http://view.szonline.net/qiwen/20200530/26614.html
http://view.szonline.net/qiwen/20200530/26613.html
http://view.szonline.net/qiwen/20200530/26612.html
http://view.szonline.net/yule/26611.html
http://view.szonline.net/gundong/20200530/26610.html
http://view.szonline.net/qiwen/20200530/26609.html
http://view.szonline.net/qiwen/20200530/26608.html
http://view.szonline.net/qiwen/20200530/26607.html
http://view.szonline.net/yule/26606.html
http://view.szonline.net/gundong/20200530/26605.html
http://view.szonline.net/qiwen/20200530/26603.html
http://view.szonline.net/qiwen/20200530/26602.html
http://view.szonline.net/qiwen/20200530/26601.html
http://view.szonline.net/yule/26600.html
以上是关于websql操作类封装的主要内容,如果未能解决你的问题,请参考以下文章