├── README.md ├── capture └── params-of-constructor.jpg ├── example ├── DrugSqliteClient.js ├── StoreSqliteClient.js └── TestSqlitePage.js ├── index.js ├── package.json └── src └── BaseSqliteClient.js /README.md: -------------------------------------------------------------------------------- 1 | # react-native-sqlite-helper-pro 2 | 使用前请先确保添加了[react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage)库并进行了link, 最好对sql语句有一定的了解, 了解基本的sql增删改查语句, 感兴趣的话建议看看[W3C SQL教程](http://www.w3school.com.cn/sql/index.asp). 3 | ## Installation 4 | 5 | yarn add react-native-sqlite-helper-pro 6 | 7 | ## How to use 8 | **下面是代码例子** 9 | 10 | ``` 11 | /** 12 | * Created by Niki on 9/8/18 12:59 AM. 13 | * Email: m13296644326@163.com 14 | */ 15 | 16 | import React from 'react'; 17 | import { 18 | View, 19 | StyleSheet, TouchableOpacity, Text, 20 | } from 'react-native'; 21 | import {BaseSqliteClient} from "../index"; 22 | 23 | export default class TestSqlitePage extends React.PureComponent { 24 | 25 | constructor(props) { 26 | super(props); 27 | this.baseSqliteClient = new BaseSqliteClient({ 28 | dbName: 'test_data.db', 29 | dbVersion: '1.0', 30 | dbDisplayName: 'TestApp', 31 | tableName: 'SHOPPING_CART_DRUG', 32 | tableCreateCommand: 33 | 'drugId INTEGER UNIQUE , ' + //drugId作为去重标志需要设置为unique 34 | 'drugStoreId INTEGER, ' + 35 | 'detail TEXT', 36 | }); 37 | this.baseSqliteClient.open(); 38 | this.baseSqliteClient.createTable(); 39 | } 40 | 41 | render() { 42 | return ( 43 | 44 | { 46 | this.baseSqliteClient.insert({drugId: 1, drugStoreId: 1, detail: 'a detail data!'}) 47 | .then(rowsAffected => { 48 | if (rowsAffected > 0){ 49 | //插入成功 50 | console.log('insert success!') 51 | }else { 52 | //插入失败 53 | console.log('insert failed!') 54 | } 55 | }); 56 | }} 57 | text={'插入数据'} 58 | /> 59 | { 61 | this.baseSqliteClient.update( 62 | //params 63 | {drugId: 2, drugStoreId: 2, detail: 'a detail data2!'}, 64 | //where 65 | {drugId: 2} 66 | ) 67 | .then(rowsAffected => { 68 | if (rowsAffected > 0){ 69 | //更新成功 70 | console.log('insert success!') 71 | }else { 72 | //更新失败 73 | console.log('insert failed!') 74 | } 75 | }); 76 | }} 77 | text={'更新数据'} 78 | /> 79 | { 81 | //插入或更新, 优先插入, 如果插入失败(数据已存在), 就更新数据 82 | //千万要注意的是, drugId这个字段必须为UNIQUE, 不然进行insert操作时无法判断是否存在重复数据, insertOrUpdate方法也就无效了 83 | this.baseSqliteClient.insertOrUpdate( 84 | //params 85 | {drugId: 2, drugStoreId: 2, detail: 'a detail data2!'}, 86 | //where 87 | {drugId: 2} 88 | ) 89 | .then(rowsAffected => { 90 | if (rowsAffected > 0){ 91 | //插入或更新成功 92 | console.log('insert success!') 93 | }else { 94 | //插入或更新失败 95 | console.log('insert failed!') 96 | } 97 | }); 98 | }} 99 | text={'插入或更新数据'} 100 | /> 101 | { 103 | //查询数据, 参数为where, 可不传, 表示查询所有数据 104 | this.baseSqliteClient.query( 105 | //where 106 | {drugId: 1} 107 | ) 108 | .then(data => { 109 | //data为数组 110 | console.log(data) 111 | }); 112 | }} 113 | text={'查询数据'} 114 | /> 115 | { 117 | //删除数据 118 | this.baseSqliteClient.deleteData( 119 | //where 120 | {drugId: 1} 121 | ) 122 | .then(data => { 123 | 124 | }); 125 | }} 126 | text={'删除数据'} 127 | /> 128 | 129 | ) 130 | } 131 | 132 | //千万别忘了close 133 | componentWillUnmount(){ 134 | this.baseSqliteClient && this.baseSqliteClient,close(); 135 | } 136 | 137 | } 138 | 139 | const BlueButton = (p) => { 140 | const {text, onPress} = p; 141 | return ( 145 | {text} 146 | ) 147 | }; 148 | 149 | 150 | const styles = StyleSheet.create({ 151 | container: { 152 | flex: 1, 153 | backgroundColor: '#fff', 154 | } 155 | }); 156 | ``` 157 | ## 参数说明 158 | **构造方法传入参数说明** 159 | 160 | | 参数名 | 默认值 | 参数说明 | 161 | | :------------------ | :----------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------- | 162 | | dbName | - | 数据库名字, 以.db为后缀, 例如tet.db | 163 | | dbVersion | 1.0 | 数据库版本号 | 164 | | dbDisplayName | | 数据库展示名称, 例如TestSqlite | 165 | | dbSize | 1 | 数据库size, 默认为-1, 表示无限制 | 166 | | tableName | - | 数据库表的表名 | 167 | | tableCreateCommand | - | 表创建命令; 框架已自动指定id主键, 使用者不要重复指定; 注意: 如果某字段是你的去重依据, 那么千万注意要将该字段设置为unique, 否则insertOrUpdate方法无法使用! | 168 | | debugMode | | 是否开启debug模式, 默认开启; 如果开启, 会在console打印一些日志 | 169 | 170 | **方法说明** 171 | 172 | | 方法名 | 参数 | 方法说明 | 173 | | -------------- | -------------------------- | ------------------------------------------------------------------------------------- | 174 | | open | - | 创建或打开数据库, 返回数据库代理对象 | 175 | | close | - | 关闭数据库通道, 解决性能 | 176 | | createTable | - | 创建表, if not exist | 177 | | insert | params | 插入一条数据 | 178 | | update | params, where | 修改一条数据 | 179 | | insertOrUpdate | params, where | 插入或更新数据; 先尝试插入, 如果有重复数据则更新数据; 注意去重标志字段需要设为unique. | 180 | | query | where | 按照条件数据, 可为空 | 181 | | executeRawSql | sql, paramsArray: 参数数组 | 执行自定义的sql语句 | 182 | | deleteData | where | 按照条件删除数据 | 183 | | _deleteTable | 删除table | 一般仅供测试使用 | 184 | 185 | ## 建议 186 | **为了保证代码的可维护性和稳定性, 建议定义一个YourSqliteClient 继承 BaseSqliteClient, 然后在YourSqliteClient中书写业务代码**
187 | **记得点赞哦~** -------------------------------------------------------------------------------- /capture/params-of-constructor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NikiLee2016/react-native-sqlite-helper-pro/37b0dd354bd2c021162cfa6bdec725edc3d92aa8/capture/params-of-constructor.jpg -------------------------------------------------------------------------------- /example/DrugSqliteClient.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Niki on 9/7/18 9:59 AM. 3 | * Email: m13296644326@163.com 4 | */ 5 | 6 | import React from 'react'; 7 | import { 8 | Platform 9 | } from 'react-native'; 10 | import BaseSqliteClient from "./BaseSqliteClient"; 11 | 12 | const isIos = Platform.OS === 'ios'; 13 | export const commonSqliteConfig = { 14 | dbName: 'rainon_yiyao_data.db', //数据库名字, 以.db为后缀, 例如tet.db 15 | dbVersion: '1.0', //数据库版本号, 例如1.0 16 | dbDisplayName: 'RainonApp', //例如TestSqlite 17 | }; 18 | 19 | export default class DrugSqliteClient extends BaseSqliteClient{ 20 | constructor() { 21 | super({ 22 | ...commonSqliteConfig, 23 | tableName: 'SHOPPING_CART_DRUG', //表名字 24 | tableCreateCommand: 25 | 'drugId INTEGER UNIQUE , ' + //drugId作为去重标志需要设置为unique 26 | 'drugStoreId INTEGER, ' + 27 | 'detail TEXT', 28 | }); 29 | this.open(); 30 | this.createTable(); 31 | } 32 | 33 | queryDrugByStoreId = (drugStoreId) => { 34 | return this.query({drugStoreId}); 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /example/StoreSqliteClient.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Niki on 9/7/18 9:59 AM. 3 | * Email: m13296644326@163.com 4 | */ 5 | 6 | import React from 'react'; 7 | import { 8 | Platform 9 | } from 'react-native'; 10 | import BaseSqliteClient from "./BaseSqliteClient"; 11 | 12 | const isIos = Platform.OS === 'ios'; 13 | export const commonSqliteConfig = { 14 | dbName: 'rainon_yiyao_data.db', //数据库名字, 以.db为后缀, 例如tet.db 15 | dbVersion: '1.0', //数据库版本号, 例如1.0 16 | dbDisplayName: 'RainonApp', //例如TestSqlite 17 | }; 18 | export default class StoreSqliteClient extends BaseSqliteClient{ 19 | constructor() { 20 | super({ 21 | ...commonSqliteConfig, 22 | tableName: 'SHOPPING_CART_DRUG_STORE', //表名字 23 | tableCreateCommand: 'drugStoreId INTEGER UNIQUE , ' //drugStoreId作为去重标志需要设置为unique 24 | + 'detail TEXT', 25 | }); 26 | this.open(); 27 | this.createTable(); 28 | } 29 | 30 | queryAllStoreData = () => { 31 | return this.query(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /example/TestSqlitePage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Niki on 9/8/18 12:59 AM. 3 | * Email: m13296644326@163.com 4 | */ 5 | 6 | import React from 'react'; 7 | import { 8 | View, 9 | StyleSheet, TouchableOpacity, Text, 10 | } from 'react-native'; 11 | import {BaseSqliteClient} from "../index"; 12 | 13 | export default class TestSqlitePage extends React.PureComponent { 14 | 15 | constructor(props) { 16 | super(props); 17 | this.baseSqliteClient = new BaseSqliteClient({ 18 | dbName: 'test_data.db', 19 | dbVersion: '1.0', 20 | dbDisplayName: 'TestApp', 21 | tableName: 'SHOPPING_CART_DRUG', 22 | tableCreateCommand: 23 | 'drugId INTEGER UNIQUE , ' + //drugId作为去重标志需要设置为unique 24 | 'drugStoreId INTEGER, ' + 25 | 'detail TEXT', 26 | }); 27 | this.baseSqliteClient.open(); 28 | this.baseSqliteClient.createTable(); 29 | } 30 | 31 | render() { 32 | return ( 33 | 34 | { 36 | this.baseSqliteClient.insert({drugId: 1, drugStoreId: 1, detail: 'a detail data!'}) 37 | .then(rowsAffected => { 38 | if (rowsAffected > 0){ 39 | //插入成功 40 | console.log('insert success!') 41 | }else { 42 | //插入失败 43 | console.log('insert failed!') 44 | } 45 | }); 46 | }} 47 | text={'插入数据'} 48 | /> 49 | { 51 | this.baseSqliteClient.update( 52 | //params 53 | {drugId: 2, drugStoreId: 2, detail: 'a detail data2!'}, 54 | //where 55 | {drugId: 2} 56 | ) 57 | .then(rowsAffected => { 58 | if (rowsAffected > 0){ 59 | //更新成功 60 | console.log('insert success!') 61 | }else { 62 | //更新失败 63 | console.log('insert failed!') 64 | } 65 | }); 66 | }} 67 | text={'更新数据'} 68 | /> 69 | { 71 | //插入或更新, 优先插入, 如果插入失败(数据已存在), 就更新数据 72 | //千万要注意的是, drugId这个字段必须为UNIQUE, 不然进行insert操作时无法判断是否存在重复数据, insertOrUpdate方法也就无效了 73 | this.baseSqliteClient.insertOrUpdate( 74 | //params 75 | {drugId: 2, drugStoreId: 2, detail: 'a detail data2!'}, 76 | //where 77 | {drugId: 2} 78 | ) 79 | .then(rowsAffected => { 80 | if (rowsAffected > 0){ 81 | //插入或更新成功 82 | console.log('insert success!') 83 | }else { 84 | //插入或更新失败 85 | console.log('insert failed!') 86 | } 87 | }); 88 | }} 89 | text={'插入或更新数据'} 90 | /> 91 | { 93 | //查询数据, 参数为where, 可不传, 表示查询所有数据 94 | this.baseSqliteClient.query( 95 | //where 96 | {drugId: 1} 97 | ) 98 | .then(data => { 99 | //data为数组 100 | console.log(data) 101 | }); 102 | }} 103 | text={'查询数据'} 104 | /> 105 | { 107 | //删除数据 108 | this.baseSqliteClient.deleteData( 109 | //where 110 | {drugId: 1} 111 | ) 112 | .then(data => { 113 | 114 | }); 115 | }} 116 | text={'删除数据'} 117 | /> 118 | 119 | ) 120 | } 121 | 122 | //千万别忘了close 123 | componentWillUnmount(){ 124 | this.baseSqliteClient && this.baseSqliteClient,close(); 125 | } 126 | 127 | 128 | } 129 | 130 | const BlueButton = (p) => { 131 | const {text, onPress} = p; 132 | return ( 136 | {text} 137 | ) 138 | }; 139 | 140 | 141 | const styles = StyleSheet.create({ 142 | container: { 143 | flex: 1, 144 | backgroundColor: '#fff', 145 | } 146 | }); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Niki on 9/8/18 12:26 AM. 3 | * Email: m13296644326@163.com 4 | */ 5 | 6 | export {default as BaseSqliteClient} from './src/BaseSqliteClient'; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-sqlite-helper-pro", 3 | "version": "1.0.2", 4 | "description": "a helper for react-native-sqlite-storage, you can get rid of raw sql with react-native-sqlite-helper-pro now!", 5 | "main": "index.js", 6 | "directories": { 7 | "example": "example" 8 | }, 9 | "scripts": { 10 | "test": "niki" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/NikiLee2016/react-native-sqlite-helper-pro.git" 15 | }, 16 | "keywords": [ 17 | "sqlite", 18 | "react-native-sqlite-storage", 19 | "storage", 20 | "react-native" 21 | ], 22 | "author": "Niki", 23 | "license": "ISC", 24 | "bugs": { 25 | "url": "https://github.com/NikiLee2016/react-native-sqlite-helper-pro/issues" 26 | }, 27 | "homepage": "https://github.com/NikiLee2016/react-native-sqlite-helper-pro#readme" 28 | } 29 | -------------------------------------------------------------------------------- /src/BaseSqliteClient.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Niki on 9/6/18 10:14 AM. 3 | * Email: m13296644326@163.com 4 | */ 5 | 6 | import React from 'react'; 7 | import { 8 | Platform 9 | } from 'react-native'; 10 | 11 | const isIos = Platform.OS === 'ios'; 12 | import SQLiteStorage from 'react-native-sqlite-storage'; 13 | 14 | const getInsertPartByObj = (params) => { 15 | let sqlPart = Object.keys(params).reduce((pre, now) => pre + ',' + now); 16 | let sqlPart2 = Object.keys(params).reduce((pre, now, index) => { 17 | if (index === 0) { 18 | return pre + '?'; 19 | } 20 | return pre + ', ?'; 21 | }, ''); 22 | return { 23 | sqlPart, 24 | sqlPart2, 25 | paramsPart: Object.values(params) 26 | } 27 | }; 28 | 29 | const getUpdatePartByObj = (params, split = ',') => { 30 | let keys = Object.keys(params); 31 | let sqlPart = keys.reduce((pre, now, index) => { 32 | if (index === 0) { 33 | return pre + `${now} = ?`; 34 | } 35 | return pre + ` ${split} ${now} = ?`; 36 | }, ''); 37 | return { 38 | sqlPart, 39 | paramsPart: Object.values(params), 40 | } 41 | }; 42 | 43 | logSuccess = (name, info) => { 44 | console.log('SQLiteStorage ' + name + ' success!'); 45 | info && console.log('successInfo: '); 46 | info && console.log(info); 47 | }; 48 | 49 | logError = (name, err) => { 50 | console.log('SQLiteStorage ' + name + ' failed!'); 51 | err && console.log('errInfo: '); 52 | console.log(err); 53 | }; 54 | 55 | getCallbacks = (action, tableName) => [ 56 | (info, results) => logSuccess(action + ' ' + tableName, results), 57 | (err) => logError(action + ' ' + tableName, err), 58 | ]; 59 | export default class BaseSqliteClient { 60 | 61 | /** 62 | * 63 | * @param dbName 数据库名字, 以.db为后缀, 例如tet.db 64 | * @param dbVersion 数据库版本号, 例如1.0 65 | * @param dbDisplayName 例如TestSqlite 66 | * @param dbSize 数据库size, 默认为-1, 表示无限制 67 | * @param tableName 表名字 68 | * @param tableCreateCommand 表创建命令, 框架已自动指定id主键, 使用者不要重复指定 69 | * 注意: 如果某字段是你的去重依据, 那么千万注意要将该字段设置为unique, 否则insertOrUpdate方法无法使用! 70 | * @param debugMode 是否开启debug模式, 默认开启; 如果开启, 会在console打印一些日志 71 | * 72 | * 注意: 如果某字段是你的去重复依据, 那么千万注意要将该字段设置为unique, 否则insertOrUpdate方法无法使用! 73 | * @param getFixedParams 获取增删改查的固定参数 74 | */ 75 | constructor({dbName, dbVersion, dbDisplayName, dbSize = -1, tableName, tableCreateCommand, debugMode = true, getFixedParams = {}}) { 76 | this.dbName = dbName; 77 | this.dbVersion = dbVersion; 78 | this.dbDisplayName = dbDisplayName; 79 | this.dbSize = dbSize; 80 | this.tableName = tableName; 81 | this.tableCreateCommand = tableCreateCommand; 82 | this.getFixedParams = getFixedParams; 83 | SQLiteStorage.DEBUG(debugMode); 84 | this.dataBase = null; 85 | } 86 | 87 | open = () => { 88 | this.dataBase = SQLiteStorage.openDatabase( 89 | this.dbName, 90 | this.dbVersion, 91 | this.dbDisplayName, 92 | this.dbSize, 93 | ...getCallbacks('open database data', this.tableName), 94 | ); 95 | return this.dataBase; 96 | }; 97 | 98 | 99 | /** 100 | * 有时候执行完了一段Sql之后, 如果立马close, 会报错. 所以有些场合需要进行safeClose, 做延时处理 101 | */ 102 | safeClose = () => { 103 | setTimeout(() => { 104 | this.close(); 105 | }, 1000); 106 | }; 107 | 108 | close = () => { 109 | this.dataBase && this.dataBase.close(); 110 | this.dataBase = null; 111 | }; 112 | 113 | createTable = () => { 114 | if (!this.dataBase) { 115 | this.open(); 116 | } 117 | this.dataBase.transaction(tx => { 118 | tx.executeSql(`CREATE TABLE IF NOT EXISTS ${this.tableName} (` + 119 | 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 120 | this.tableCreateCommand 121 | + ')', 122 | [], 123 | () => logSuccess('create table ' + this.tableName), 124 | (err) => this.logError('create table ' + this.tableName, err) 125 | ); 126 | }, 127 | ...getCallbacks('create table', this.tableName), 128 | ); 129 | }; 130 | 131 | insert = params => { 132 | Object.assign(params, this.getFixedParams()); 133 | const {sqlPart, sqlPart2, paramsPart} = getInsertPartByObj(params); 134 | //先update, 如果row小于0, 那么insert 135 | if (!this.dataBase) { 136 | this.open(); 137 | } 138 | return new Promise((res, rej) => { 139 | this.dataBase.transaction(tx => { 140 | tx.executeSql( 141 | `INSERT INTO ${this.tableName} (${sqlPart}) VALUES (${sqlPart2})`, 142 | paramsPart, 143 | (info, results) => res(results.rowsAffected), 144 | (err) => { 145 | getCallbacks('insert data', this.tableName)[1](err); 146 | //todo ios平台不一定报这个错 147 | if (err && err.message && err.message.indexOf('UNIQUE constraint failed') >= 0) { 148 | //差不多就是报错的意思 149 | res(0); 150 | } 151 | }, 152 | ); 153 | }, 154 | ...getCallbacks('insert data', this.tableName), 155 | ) 156 | }); 157 | }; 158 | 159 | update = (params, where = {}) => { 160 | Object.assign(where, this.getFixedParams()); 161 | const {sqlPart, paramsPart} = getUpdatePartByObj(params); 162 | const {sqlPart: sqlPartWhere, paramsPart: paramsPartWhere} = getUpdatePartByObj(where, 'AND'); 163 | return new Promise((res, rej) => { 164 | this.dataBase.transaction(tx => { 165 | let data = tx.executeSql( 166 | `UPDATE ${this.tableName} SET ${sqlPart} WHERE ${sqlPartWhere}`, 167 | [...paramsPart, ...paramsPartWhere], 168 | (info, results) => res(results.rowsAffected), 169 | getCallbacks('update data', this.tableName)[1], 170 | ); 171 | console.log(data); 172 | }, 173 | ...getCallbacks('update data', this.tableName), 174 | ) 175 | }) 176 | }; 177 | 178 | insertOrUpdate = (params, where = {}) => { 179 | //StringUtil.translateObj2String(this.getFixedParams()); 180 | Object.assign(where, this.getFixedParams()); 181 | Object.assign(params, this.getFixedParams()); 182 | return this.insert(params) 183 | .then(rowsAffected => { 184 | //如果顺利插入 185 | if (rowsAffected > 0) { 186 | return rowsAffected; 187 | } 188 | //如果由于重复无法顺利插入 189 | else { 190 | return this.update(params, where); 191 | } 192 | }); 193 | }; 194 | 195 | /** 196 | * 通过query的结果来判断是否重复, 效率稍慢, 但是去重字段可以不用设置为unique 197 | * @param params 198 | * @param where 199 | * @returns {Promise.|*|Promise.<*>|axios.Promise} 200 | */ 201 | insertOrUpdateByQuery = (params, where = {}) => { 202 | Object.assign(where, this.getFixedParams()); 203 | Object.assign(params, this.getFixedParams()); 204 | return this.query(where) 205 | .then((data) => { 206 | if (data && data.length > 0) { 207 | return this.update(params, where); 208 | } else { 209 | return this.insert(params); 210 | } 211 | }); 212 | }; 213 | 214 | 215 | query = (where = {}) => { 216 | Object.assign(where, this.getFixedParams()); 217 | let wherePart = ''; 218 | let whereParams = []; 219 | if (where && JSON.stringify(where) !== '{}') { 220 | const {sqlPart, paramsPart} = getUpdatePartByObj(where, 'AND'); 221 | wherePart = 'WHERE ' + sqlPart; 222 | whereParams = paramsPart; 223 | } 224 | return new Promise((res, rej) => { 225 | this.dataBase.transaction(tx => { 226 | tx.executeSql( 227 | `SELECT * FROM ${this.tableName} ${wherePart}`, 228 | whereParams, 229 | (info, results) => { 230 | let finalData = []; 231 | if (results && results.rows && results.rows.length > 0) { 232 | for (let i = 0; i < results.rows.length; i++) { 233 | finalData.push(results.rows.item(i)); 234 | } 235 | } 236 | res(finalData); 237 | }, 238 | getCallbacks('insert data', this.tableName)[1], 239 | ); 240 | }, 241 | ...getCallbacks('select data', this.tableName), 242 | ) 243 | }); 244 | }; 245 | 246 | executeRawSql = (sqlStr: string, params?: Array) => { 247 | return new Promise((res, rej) => { 248 | this.dataBase.transaction(tx => { 249 | tx.executeSql( 250 | sqlStr, 251 | params, 252 | (info, results) => { 253 | res(results); 254 | }, 255 | err => rej(err), 256 | ); 257 | }, 258 | ...getCallbacks('raw sql', this.tableName), 259 | ) 260 | }); 261 | }; 262 | 263 | deleteData = (where) => { 264 | Object.assign(where, this.getFixedParams()); 265 | const {sqlPart, paramsPart} = getUpdatePartByObj(where, 'AND'); 266 | return new Promise((res, rej) => { 267 | this.dataBase.transaction(tx => { 268 | tx.executeSql( 269 | `DELETE FROM ${this.tableName} WHERE ${sqlPart}`, 270 | paramsPart, 271 | (info, results) => res(results.rowsAffected), 272 | getCallbacks('update data', this.tableName)[1], 273 | ); 274 | }, 275 | ...getCallbacks('delete data', this.tableName), 276 | ) 277 | }) 278 | }; 279 | 280 | _deleteTable = (tableName) => { 281 | this.dataBase.transaction(tx => { 282 | tx.executeSql( 283 | `drop table ${tableName}`, 284 | [], 285 | ...getCallbacks('delete table', this.tableName), 286 | ) 287 | }, 288 | ...getCallbacks('delete table', this.tableName), 289 | ) 290 | } 291 | 292 | } 293 | --------------------------------------------------------------------------------