Storage
export async function getValueByKey(key,
callback = () => {
},
callbackError = () => {
ViewUtil.showToast(key + ' not found!');
}) {
try {
var ret = await storage.load({
key: key,
});
if (callback) {
callback(ret);
}
return ret;
} catch (e) {
callbackError();
}
}
export async function setKeyValue( key,
value,
callbackError = () => {
ViewUtil.showToast(key + ':' + value + ' save error!');
}) {
try {
storage.save({ key: key, data: value });
} catch (e) {
callbackError();
}
}
Sqlite
SQLiteUtil.js
/**
* Created by nick on 2017/6/9.
*/
import React, { Component } from 'react';
import SQLiteStorage from 'react-native-sqlite-storage';
import * as ConstantUtil from './ConstantUtil';
import * as ViewUtil from './ViewUtil';
import * as StorageUtil from './StorageUtil';
SQLiteStorage.DEBUG(ConstantUtil.config.sqliteDebuggable);
SQLiteStorage.enablePromise(false);
var database_name = ConstantUtil.config.sqliteDbName;//数据库文件
var database_version = ConstantUtil.config.sqliteDbVersion;//版本号
var database_displayname = ConstantUtil.config.sqliteDbDisplayName;
var database_size = ConstantUtil.config.sqliteDbSize;//-1应该是表示无限制
var db;
export default class SQLiteUtil extends Component {
//TODO 数据库开关
componentWillUnmount() {
if (db) {
this._successCB('close');
db.close();
} else {
console.log('SQLiteStorage not open');
}
}
open() {
// {name: 'testDB', createFromLocation : 1},
db = SQLiteStorage.openDatabase(
database_name,
database_version,
database_displayname,
database_size,
() => {
this._successCB('open');
},
(err) => {
this._errorCB('open', err);
});
return db;
}
createTable() {
if (!db) {
this.open();
}
console.log(db);
//创建用户表
db.transaction((tx) => {
tx.executeSql('CREATE TABLE IF NOT EXISTS t_order (' +
'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'orderId VARCHAR,' +
'createTime VARCHAR,' +
'nowHolderId VARCHAR,' +
'nowHolderName VARCHAR,' +
'lastHolderId VARCHAR,' +
'lastHolderName VARCHAR)'
, [], () => {
this._successCB('executeSql');
}, (err) => {
this._errorCB('executeSql', err);
});
}, (err) => {//所有的 transaction都应该有错误的回调方法,在方法里面打印异常信息,不然你可能不会知道哪里出错了。
this._errorCB('transaction', err);
}, () => {
this._successCB('transaction');
});
}
deleteData() {
if (!db) {
this.open();
}
db.transaction((tx) => {
tx.executeSql('delete from t_order', [], () => {
});
});
}
dropTable() {
db.transaction((tx) => {
tx.executeSql('drop table t_order', [], () => {
});
}, (err) => {
this._errorCB('transaction', err);
}, () => {
this._successCB('transaction');
});
}
close() {
if (db) {
this._successCB('close');
db.close();
} else {
console.log('SQLiteStorage not open');
}
db = null;
}
_successCB(name) {
console.log('SQLiteStorage ' + name + ' success');
}
_errorCB(name, err) {
console.log('SQLiteStorage ' + name);
console.log(err);
}
render() {
return null;
}
//TODO 数据库升级
checkDataBaseUpdata() {
StorageUtil.getValueByKeyDbVersion((ret) => {
console.log(ret);
let version = ret;
if (version < database_version) {
switch (version) {
case 1:
this.migrateV1toV2();
// case 2:
// migrateV2toV3();
}
StorageUtil.setValueOfDbVersion(database_version);
}
});
}
migrateV1toV2() {
this.createTableOrderMessageCountAndTaskReportCount();
}
createTableOrderMessageCountAndTaskReportCount() {
if (!db) {
this.open();
}
console.log(db);
//创建用户表
db.transaction((tx) => {
tx.executeSql('CREATE TABLE IF NOT EXISTS tOrderMessageCountAndTaskReportCount (' +
'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'orderId VARCHAR,' +
'messageCount INTEGER,' +
'taskReportCount INTEGER)'
, [], () => {
this._successCB('executeSql');
}, (err) => {
this._errorCB('executeSql', err);
});
}, (err) => {//所有的 transaction都应该有错误的回调方法,在方法里面打印异常信息,不然你可能不会知道哪里出错了。
this._errorCB('transaction', err);
}, () => {
this._successCB('transaction');
});
}
//todo 增删改查
insertOrderData(orderData) {
let len = orderData.length;
if (!db) {
this.open();
}
this.createTable();
// this.deleteData();
db.transaction((tx) => {
for (let i = 0; i < len; i++) {
let order = orderData[i];
if (
order
&& order.orderId
&& order.createTime
) {
let orderId = order.orderId;
let createTime = order.createTime;
let nowHolderId = order.nowHolderId;
let nowHolderName = order.nowHolderName;
let lastHolderId = order.lastHolderId;
let lastHolderName = order.lastHolderName;
let sql = 'INSERT INTO t_order(orderId,createTime,nowHolderId,nowHolderName,lastHolderId,lastHolderName)' +
'values(?,?,?,?,?,?)';
tx.executeSql(sql, [orderId, createTime, nowHolderId, nowHolderName, lastHolderId, lastHolderName], () => {
}, (err) => {
console.log(err);
}
);
}
}
}, (error) => {
this._errorCB('transaction', error);
ViewUtil.showToast('数据插入失败');
}, () => {
this._successCB('transaction insert data');
ViewUtil.showToast('成功插入 ' + len + ' 条工单数据');
});
}
selectAllOrderData(callback) {
var orderData = [];
let len = orderData.length;
if (!db) {
this.open();
}
// this.createTable();
db.transaction((tx) => {
tx.executeSql('SELECT * FROM t_order', [], (tx, results) => {
console.log('Query completed');
console.log(tx);
console.log(results);
callback(results.rows);
// Get rows with Web SQL Database spec compliance.
//
// var len = results.rows.length;
// for (let i = 0; i < len; i++) {
// let row = results.rows.item(i);
// console.log(`Record: ${row.name}`);
// }
});
});
}
updateOrInsertOrderData(orderData) {
if (!db) {
this.open();
}
this.createTable();
// this.deleteData();
db.transaction((tx) => {
let order = orderData;
if (
order
&& order.orderId
&& order.createTime
) {
let orderId = order.orderId;
let createTime = order.createTime;
let nowHolderId = order.nowHolderId;
let nowHolderName = order.nowHolderName;
let lastHolderId = order.lastHolderId;
let lastHolderName = order.lastHolderName;
tx.executeSql('SELECT * FROM t_order where orderId=?', [orderId], (tx, results) => {
console.log('Query completed');
console.log(tx);
console.log(results);
if (0 < results.rows.length) {//update
let sql = 'UPDATE t_order SET createTime = ? nowHolderId = ? nowHolderName = ? lastHolderId = ? lastHolderName = ? WHERE orderId = ?;';
tx.executeSql(sql, [createTime, nowHolderId, nowHolderName, lastHolderId, lastHolderName, orderId], () => {
}, (err) => {
console.log(err);
}
);
} else {//insert
let sql = 'INSERT INTO t_order(orderId,createTime,nowHolderId,nowHolderName,lastHolderId,lastHolderName)' +
'values(?,?,?)';
tx.executeSql(sql, [orderId, createTime, nowHolderId, nowHolderName, lastHolderId, lastHolderName], () => {
}, (err) => {
console.log(err);
}
);
}
});
}
}, (error) => {
this._errorCB('transaction', error);
ViewUtil.showToast('数据插入失败');
}, () => {
this._successCB('transaction insert data');
ViewUtil.showToast('成功插入工单数据');
});
}
//TableOrderMessageCountAndTaskReportCount
selectFromTableOrderMessageCountAndTaskReportCountByOrderId(callback, orderId) {
// return;
let orderData = { orderId: orderId, messageCount: 0, taskReportCount: 0 };
if (!db) {
this.open();
}
db.transaction((tx) => {
tx.executeSql('SELECT * FROM tOrderMessageCountAndTaskReportCount where orderId=?', [orderId], (tx, results) => {
console.log('Query completed');
console.log(tx);
console.log(results);
callback(results.rows);
// Get rows with Web SQL Database spec compliance.
//
// var len = results.rows.length;
// for (let i = 0; i < len; i++) {
// let row = results.rows.item(i);
// console.log(`Record: ${row.name}`);
// }
});
});
}
updateOrInsertDataTableOrderMessageCountAndTaskReportCount(orderData) {
// return ;
if (!db) {
this.open();
}
this.createTableOrderMessageCountAndTaskReportCount();
db.transaction((tx) => {
let order = orderData;
if (
order
&& order.orderId
) {
let orderId = order.orderId;
let messageCount = order.messageCount || 0;
let taskReportCount = order.taskReportCount || 0;
tx.executeSql('SELECT * FROM tOrderMessageCountAndTaskReportCount where orderId=?', [orderId], (tx, results) => {
console.log('Query completed');
console.log(tx);
console.log(results);
console.log(order);
if (0 < results.rows.length) {//update
let sql = 'UPDATE tOrderMessageCountAndTaskReportCount SET messageCount = ?, taskReportCount = ? WHERE orderId = ?;';
console.log(sql);
tx.executeSql(sql, [messageCount, taskReportCount, orderId], () => {
global.TmpDataUtil.orderMessageCountOld = messageCount;
global.TmpDataUtil.orderTaskReportCountOld = taskReportCount;
}, (err) => {
console.log(err);
}
);
} else {//insert
let sql = 'INSERT INTO tOrderMessageCountAndTaskReportCount(orderId,messageCount,taskReportCount)' +
'values(?,?,?)';
console.log(sql);
tx.executeSql(sql, [orderId, messageCount, taskReportCount], () => {
global.TmpDataUtil.orderMessageCountOld = messageCount;
global.TmpDataUtil.orderTaskReportCountOld = taskReportCount;
}, (err) => {
console.log(err);
}
);
}
});
}
}, (error) => {
this._errorCB('transaction', error);
ViewUtil.showToast('数据插入失败');
}, () => {
this._successCB('transaction insert data');
// ViewUtil.showToast('成功插入工单数据');
});
}
}
ConstantUtil.js
var ConstantUtil = {
config: {
sqliteDebuggable: false,
sqliteDbDisplayName: 'testDb',
sqliteDbName: 'test.db',
sqliteDbVersion: 2,
sqliteDbSize: -1,//-1应该是表示无限制
},
};
module.exports = ConstantUtil;