var db;

function fnInitLocal() {
    db = api.require('db');
};

// 数据库名称
var DATABASE = 'database_pandian4';
// 表名
var PANDIAN = 'table_pandian';

function fnOpenDatabase() {
    fnInitLocal();
    db.openDatabase({
        name: DATABASE
    }, function(ret, err) {

        fnCreatePandian(function() {
            fnCreatePandianIndex();
        });
    });
};



function fnRemovePandian(callback) {
    db.executeSql({
        name: DATABASE,
        sql: 'CREATE TABLE ' + PANDIAN + '(id INTEGER PRIMARY KEY, pandianId VARCHAR(64), jsonCon VARCHAR(6000))'
    }, callback);
};

function fnCreatePandian(callback) {
    db.executeSql({
        name: DATABASE,
        sql: 'CREATE TABLE ' + PANDIAN + '(id INTEGER PRIMARY KEY, pandianId VARCHAR(64), jsonCon VARCHAR(64))'
    }, callback);
};

function fnCreatePandianIndex(callback) {
    db.executeSql({
        name: DATABASE,
        sql: 'CREATE UNIQUE INDEX INDEX_' + PANDIAN + '_pandianId ON ' + PANDIAN + '(pandianId)'
    }, callback);
};


function fnInsertPandian(pandianId, Json,callback) {
    db.executeSql({
        name: DATABASE,
        sql: 'REPLACE INTO ' + PANDIAN + '(pandianId,jsonCon) VALUES("' + pandianId + '","' + encodeURIComponent(Json) + '")'
    }, callback);
};

function fnSelectPandian(callback) {
    db.selectSql({
        name: DATABASE,
        sql: 'SELECT id,pandianId,jsonCon FROM ' + PANDIAN
    }, callback);
};

function fnSelectPandianById(pandianId,callback) {
    db.selectSql({
        name: DATABASE,
        sql: 'SELECT id,pandianId,jsonCon FROM ' + PANDIAN+' WHERE pandianId = "'+pandianId+'"  '
    }, callback);
};

function fnDelectPandian(callback) {
    db.executeSql({
        name: DATABASE,
        sql: 'DELETE FROM ' + PANDIAN+' '
    }, callback);
};
