0

有人知道如何将来自服务器的 jsonp 数据存储在 phonegap 本地数据库中吗?

下面的代码可以帮助将 phonegap android 应用程序连接到服务器,但是如何将数据存储在 phonegap 本地数据库中?

$.ajax({
    url: 'http://172.18.75.156/deals.php',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function(data, status){
        $.each(data, function(i,item){ 
            output.text('successful');
        });
    },
    error: function(){
       output.text('There was an error loading the data.');
    }
});
4

6 回答 6

1
  db = window.openDatabase("SQL", 3, "PhoneGap Demo", 200000);

db.transaction(ajex_call, errorCB);

    function ajex_call(tx) {
$.ajax({
url: 'http://172.18.75.156/deals.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
    $.each(data, function(i,item){ 
    //item.obj
        tx.executeSql("INSERT OR REPLACE INTO table-name(table-fields) values(?,?,..)",               [array-data])
    });
       },
error: function(){
   output.text('There was an error loading the data.');
}
});
}

有关本地数据库的更多信息http://docs.phonegap.com/en/2.2.0/cordova_storage_storage.md.html

于 2012-12-18T09:30:38.633 回答
1

像这样尝试,希望这会奏效:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    db = window.openDatabase("SQL", 3, "PhoneGap Demo", 200000);
    db.transaction(ajex_call, success, errorCB);
}
function ajex_call(tx) {
    tx.executeSql('DROP TABLE IF EXISTS table_name');
    tx.executeSql('CREATE TABLE IF NOT EXISTS table_name (fields_required_for_table)');
    $.ajax({ url: 'http://172.18.75.156/deals.php', dataType: 'jsonp', jsonp: 'jsoncallback', timeout: 5000, success: function(data, status){
        $.each(data, function(i,item){
            tx.executeSql("INSERT OR REPLACE INTO table-name(table-fields) values(?,?,..)")
        });
    }, error: function(){
        output.text('There was an error loading the data.');
    } 
}); 
}

function success(){
    console.log('Success');
}
function error(){
    console.log('error');
}
于 2015-08-27T17:53:35.880 回答
0

查看 HTML5 的本地存储。

PhoneGap 的文档在这里

于 2012-11-04T02:38:50.880 回答
0

使用数组存储来自 JSON 导入的数据。然后将数组保存到本地存储。

$.ajax({
url: 'http://172.18.75.156/deals.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
    var ArrayName = []; 
    $.each(data, function(i,item){ 
        output.text('successful');
        ArrayName[i] = item;
    });
    localStorage.setItem("jsontable",ArrayName);
},
error: function(){
   output.text('There was an error loading the data.');
}
});

然后你可以使用调用该数组localStorage.GetItem("jsontable");

然后用户将能够使用导入的 json 表数组而无需重新导入。

于 2016-03-09T23:22:19.370 回答
0

我建议您将对象转换为字符串,然后将其保存在 localStorage 中。

要检索数据,请从 localStorage 获取字符串并将其转换为 JSON 对象

HTML5 本地存储

于 2017-06-25T04:08:28.017 回答
0

很久以前我为这种东西做了一个基本的数据库控制器类,设法找到它,希望它能给你一个想法。

一旦你把DataBaseCtrl代码放在某个地方,你就可以像这样使用它:

var myDatabase = DataBaseCtrl();
myDatabase.initWithConfig("DBShortName", "1.0", "MyDbName", 10000);
myDataBase.executeSql("SQL commands here...");

在您的情况下,取决于您的数据如何设置您的表格

myDataBase.executeSql("CREATE TABLE IF NOT EXISTS LOGS (id unique, log)");
myDataBase.executeSql("INSERT INTO LOGS (id, log) VALUES (1, 'foobar')");
myDataBase.executeSql("INSERT INTO LOGS (id, log) VALUES (2, 'logmsg')");

然后也许使用循环来获取所有数据:

for (i = 0; i < data.length; i += 1) {
    myDataBase.executeSql("INSERT INTO LOGS (id, log) VALUES ("+i+", "+data[i]+")");
}

这是其余的方法

    myDataBase.init(); // uses set/default config
    myDataBase.initWithConfig(shortName, version, displayName, maxSize);
    myDataBase.executeSql(SqlCmmndString);
    myDataBase.executeSqlWithCallBack(SqlCmmndString,SuccessCallbackfunction); // how you get data out
    myDataBase.setInitConfig(shortName, version, displayName, maxSize);

这是类代码:

 var DataBaseCtrl = function () {

    if (!(this instanceof DataBaseCtrl)) {
        return new DataBaseCtrl();
    }

    // Transaction error callback
    function errorCB(tx, err) {
        console.log("Error processing SQL: " + tx + tx.code + tx.message);
    }

    function successCB(tx, err) {
    }

    return {
        _DB: null,
        _config: {
            // Default configuration
            _shortName: "DefaultDataBaseName",
            _version: "1.0",
            _displayName: "DisplayName",
            _maxSize: 65535 // in MBs
        },
        /* Initializer */
        init: function () {
            if (!window.openDatabase) {
                alert("Databases are not supported on this device. \n\n ");
                return false;
            }

            var cfg = {
                shrt: this._config._shortName,
                vers: this._config._version,
                disp: this._config._displayName,
                mxSz: this._config._maxSize
            };
            // Initialize the DataBase.
            this._DB = window.openDatabase(cfg.shrt, cfg.vers, cfg.disp, cfg.mxSz);
        },
        /* Initialize with custom config */
        initWithConfig: function (shortName, version, displayName, maxSize) {
            this.setInitConfig(shortName, version, displayName, maxSize);
            this.init();
        },
        /* Execute SQL command */
        executeSql: function (SqlCmmnd) {
            this._DB.transaction(function (tx) {
                console.log("Executing SQL... " + SqlCmmnd.substring(0, 100));
                tx.executeSql(SqlCmmnd);
            }, errorCB, successCB);
        },
        /* Execute SQL with success callback */
        executeSqlWithCallBack: function (SqlCmmnd, SuccessCallback) {
            this._DB.transaction(function (tx) {
                console.log("Executing SQL... " + SqlCmmnd.substring(0, 100));
                tx.executeSql(SqlCmmnd, [], SuccessCallback);
            }, errorCB, successCB);
        },
        /* Sets init config (call before initializing) */
        setInitConfig: function (shortName, version, displayName, maxSize) {
            console.log("Setting DB Config: " + displayName);
            this._config = {
                _shortName: shortName,
                _version: version,
                _displayName: displayName,
                _maxSize: maxSize
            };
        }
    };
}; 
于 2015-12-05T10:02:55.823 回答