indexeddb实例

//存储发送到后台的数据
var sendArr=[];
//创建本地数据库的相关数据
var myDB={
    name:'test',
    version:1,
    table:"records",
    db:null
};
//客户端签名与发送的时间差
var start_time;
var end_time;
/**
 * 初始化本地数据库
 */
$(function(){
    openDB(myDB.name,myDB.version)
});
/**
 *
 * 创建数据库
 * @param name
 * @param version
 */
function openDB (name,version) {
    var version=version || 1;
    var request=window.indexedDB.open(name,version);
    request.onerror=function(e){
        console.log(e.currentTarget.error.message);
    };
    request.onsuccess=function(e){
        myDB.db=e.target.result;
    };
    request.onupgradeneeded=function(e){
        var db=e.target.result;
        if(!db.objectStoreNames.contains(myDB.table)){
            db.createObjectStore(myDB.table,{autoIncrement: true});
            //自定义主键
            // db.createObjectStore(myDB.table,{keyPath:"id"});
        }
    };
}

/**
 * 往数据库表中添加数据
 * @param db
 * @param storeName
 * @param datas
 */
function addData(db,storeName,datas){
    var store=db.transaction(storeName,'readwrite').objectStore(storeName);
    start_time=new Date().getTime();
    for(var i=0;i/**
 * 获取表中的数据 通过主键获取
 * @param db
 * @param storeName
 * @param key
 */
function getDataByKey(db,storeName,value){
    var request=db.transaction(storeName,'readwrite').objectStore(storeName).get(value);
    request.onsuccess=function(e){
        var record=e.target.result;
    };
}
/**
 * 更改数据
 * @param db
 * @param storeName
 * @param value
 */
function updateDataByKey(db,storeName,value){
    var request=db.transaction(storeName,'readwrite').objectStore(storeName).get(value);
    request.onsuccess=function(e){
        var record=e.target.result;
        record.type="签名更改";
        store.put(record);
    };
}

//删除某条记录
function deleteDataByKey(db,storeName,value){
    db.transaction(storeName,'readwrite').objectStore(storeName).delete(value);
}


/**
 * 清空数据
 * @param db
 * @param storeName
 */
function clearObjectStore(db,storeName){
    db.transaction(storeName,'readwrite').objectStore(storeName).clear();
}

/**
 * 通过游标遍历数据库的全部数据
 * @param db
 * @param storeName
 * @param done 这里的done是回调函数,因为我是将数据放入后台队列中,
 *             在传输因为异步的问题,所以我将ajax放入回调函数中。
 */
function fetchStoreByCursor(db,storeName,done){
    end_time=new Date().getTime();
    db.transaction(storeName).objectStore(storeName).openCursor().onsuccess=function(event){
        var cursor = event.target.result;
        if (cursor) {
            var record=cursor.value;
            record.by1=end_time-start_time;
            sendArr.push(cursor.value);
            cursor.continue();
        }else{
            done()
        }
    }
}

/**
 * 清除数据
 */
function clearData(){
    clearObjectStore(myDB.db,myDB.table);
    sendArr=[];
    console.log("clear");
}

/**
 * 转换时间格式为 2015-03-19
 * @param date
 * @returns {string}
 */
var formatDate = function (date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d;
};

/**
 * 2015-03-19 12:00
 * @param date
 * @returns {string}
 */
var formatDateTime = function (date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    return y + '-' + m + '-' + d+' '+h+':'+minute;
};

你可能感兴趣的:(javascript)