import React,{Component} from 'react'
import{
} from 'react-native'
import SQLiteManager from 'react-native-sqlite-storage'
import {DB_name} from './sqlitConfig'
SQLiteManager.DEBUG(true)
let DB = undefined // 数据库对象
/**
* [initDB 创建数据库]
* @param {[type]} userID [用户id,每一个用户独立对应一个数据库]
*/
function initDB(userID) {
try {
DB = SQLiteManager.openDatabase(
{
name: DB_name + userID +'.db', // 数据库名称
location: 'Documents' // 仅支持ios,DB在android的位置是固定的,在ios需要指定位置,默认 default(Library/LocalDatabase)
},
initDBSuccess,
initDBError
)
} catch (e) {
console.log('initDB error =', e)
} finally {
}
}
/**
* [initDBSuccess 创建数据库成功输出]
*/
function initDBSuccess(){
console.log('initDBSuccess')
}
/**
* [initDBError 创建数据库失败输出]
*/
function initDBError(err){
console.log(DB_name + 'initDBError error =',err)
}
/**
* [closeDB 关闭数据库]
*/
function closeDB(){
if(DB){
DB.close()
console.log(DB_name + 'close success')
}else {
console.log(DB_name + 'not open')
}
}
/**
* [createTable 创建表]
* @param {[type]} sql [sql 语句]
* @param {[type]} callBack [回调 true, data ]
*/
function createTable(sql, callBack) {
console.log(`createTable is =${sql}`)
DB.transaction((tx) => {
tx.executeSql(
sql,
[],
() => {
callBack && callBack(true)
console.log(`createTable executeSql success`)
},
(err) => {
callBack && callBack(false, err)
console.log('createTable executeSql error=',err)
})
},
(err) => {
console.log('createTable transaction error=',err)
},
() => {
console.log(`createTable transaction success`)
})
}
/**
* [insertDataToTable 插入或者更新数据]
* @param {[type]} tableName [表名]
* @param {[type]} data [数据]
*/
function insertDataToTable(tableName, data, callBack){
let sql = `INSERT OR REPLACE INTO ${tableName} (${Object.keys(data).join(',')}) VALUES (${Array(Object.keys(data).length).fill('?').join(',')})`
console.log(`insertDataToTable ${tableName} sql is =${sql}`)
DB.transaction((tx) => {
tx.executeSql(
sql,
Object.values(data),
() => {
callBack && callBack(true)
console.log(`insertDataToTable ${tableName} executeSql success`)
},
(err) => {
callBack && callBack(false, err)
console.log('insertDataToTable executeSql error=',err)
})
},
(err) => {
console.log('insertDataToTable transaction error=',err)
},
() => {
console.log(`insertDataToTable ${tableName} transaction success`)
})
}
/**
* [deleteDataFromTable 根据id从表中删除某一条数据]
* @param {[type]} tableName [表名]
* @param {[type]} key [字段名称,要根据此字段进行删除对应的数据]
* @param {[type]} value [要删除数据的唯一标识]
*/
function deleteDataFromTable(tableName, key, value, callBack){
let sql = `DELETE FROM ${tableName} WHERE ${key} = ${value}`
console.log(`deleteDataFromTable ${tableName} sql is =${sql}`)
DB.transaction((tx) => {
tx.executeSql(
sql,
[],
() => {
callBack && callBack(true)
console.log(`deleteDataFromTable ${tableName} executeSql success`)
},
(err) => {
callBack && callBack(false, err)
console.log('deleteDataFromTable executeSql error=',err)
})
},
(err) => {
console.log('deleteDataFromTable transaction error=',err)
},
() => {
console.log(`deleteDataFromTable ${tableName} transaction success`)
})
}
/**
* [selectDataFromTable 查询表中所有数据]
* @param {[type]} tableName [表名]
*/
function selectDataFromTable(tableName, callBack){
let sql = `SELECT * FROM ${tableName}`
console.log(`selectDataFromTable ${tableName} sql is =${sql}`)
DB.transaction((tx) => {
tx.executeSql(
sql,
[],
(tx,results) => {
let datas = [];
for(let i = 0; i < results.rows.length; i++){
let info = results.rows.item(i);
datas.push(info)
}
callBack && callBack(true, datas)
console.log(`selectDataFromTable ${tableName} executeSql success`)
},
(err) => {
callBack && callBack(false, err)
console.log('selectDataFromTable executeSql error=',err)
})
},
(err) => {
console.log('selectDataFromTable transaction error=',err)
},
() => {
console.log(`selectDataFromTable ${tableName} transaction success`)
})
}
/**
* [getMsgInfoFromTable 根据id获取某一条信息]
* @param {[type]} tableName [表名]
* @param {[type]} key [字段名称,要根据此字段进行获取对应的数据]
* @param {[type]} value [要获取数据的唯一标识]
*/
function getMsgInfoFromTable(tableName, key, value, callBack){
let sql = `SELECT * FROM ${tableName} WHERE ${key} = ${value}`
console.log(`getMsgInfoFromTable ${tableName} sql is =${sql}`)
DB.transaction((tx) => {
tx.executeSql(
sql,
[],
(tx,results) => {
callBack && callBack(true, results.rows.item(0))
console.log(`getMsgInfoFromTable ${tableName} executeSql success`)
},
(err) => {
callBack && callBack(false, err)
console.log('getMsgInfoFromTable executeSql error=',err)
})
},
(err) => {
console.log('getMsgInfoFromTable transaction error=',err)
},
() => {
console.log(`getMsgInfoFromTable ${tableName} transaction success`)
})
}
/**
* [dropTable 删除表]
* @param {[type]} tableName [表名]
*/
function dropTable(tableName, callBack){
let sql = `DROP TABLE ${tableName}`
console.log(`dropTable ${tableName} sql is =${sql}`)
DB.transaction((tx) => {
tx.executeSql(
sql,
[],
(tx,results) => {
callBack && callBack(true)
console.log(`dropTable ${tableName} executeSql success`)
},
(err) => {
callBack && callBack(false, err)
console.log('dropTable executeSql error=',err)
})
},
(err) => {
console.log('dropTable transaction error=',err)
},
() => {
console.log(`dropTable ${tableName} transaction success`)
})
}
/**
* [customSQL 自定义sql语句]
* @param {[type]} sql [sql语句]
*/
function runCustomSQL(sql, callBack){
DB.transaction((tx) => {
tx.executeSql(
sql,
[],
(tx,results) => {
console.log(`runCustomSQL executeSql success`)
if (results && results.rows){
let datas = [];
for(let i = 0; i < results.rows.length; i++){
let info = results.rows.item(i);
datas.push(info)
}
callBack && callBack(true, datas)
return
}
callBack && callBack(true)
},
(err) => {
callBack && callBack(false, err)
console.log('runCustomSQL executeSql error=',err)
})
},
(err) => {
console.log('runCustomSQL transaction error=',err)
},
() => {
console.log(`runCustomSQL transaction success`)
})
}
export default {
initDB,
closeDB,
createTable,
insertDataToTable,
deleteDataFromTable,
selectDataFromTable,
getMsgInfoFromTable,
dropTable,
runCustomSQL
}
RN- react-native-sqlite-storage 封装增删改查方法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- throw err;^Error:Cannotfindmodule'react-native/setupBabel...
- 1.需要用到的jar包commons-beanutils-1.8.3.jarmysql-connector-jav...
- Storage Sqlite SQLiteUtil.js ConstantUtil.js