Get data from OData with datajs and save to sqlite in phonegap

I am developing an app in phonegap which get data from OData with datajs and save it to sqlite. datajs works fine, but when I try to save data to sqlite I get the error " E/Web Console(11533): Uncaught Error: INVALID_STATE_ERR: DOM Exception 11 at file:///android_asset/www/js/core/Sincronizacion.js:19"

This is the code:

function SincronizarClientes()  // This function runs after a button click
{
    var db = window.openDatabase("TestDb", "1.0", "TestDb - 1", 2000000);
    db.transaction(ActualizarClientes, FalloSincronizacion, OkSincronizacion);  
}  


function ActualizarClientes(tx)
{

    OData.read("http://192.168.0.4:30003/TeyunaServices.svc/Clientes?$filter=EQUIPO eq 'T1' and startswith(NOM_UCLIENTE, 'MIGUEL') eq true",
    function (data, request) {
        var html = "", sql = "";
        for (var i = 0; i < data.results.length; i++) {
            html += "<div>" + data.results[i].NOM_UCLIENTE + "</div>";
            sql = "INSERT OR REPLACE INTO Clientes (Cod_UCliente, Nom_UCliente, Fecha_Actualizacion) VALUES ('" + data.results[i].COD_UCLIENTE + 
                          "','" + data.results[i].NOM_UCLIENTE + "', datetime())";
            //sql = "INSERT OR REPLACE INTO Clientes (Cod_UCliente, Nom_UCliente, Fecha_Actualizacion) VALUES ('00012125', 'PEDRO PEREZ PINTO', datetime())";              
            tx.executeSql(sql);  //This is line 19
            alert(sql);
        }
        document.getElementById("mainContent").innerHTML = sql;

        alert("Los clientes se actualizaron correctamente!");
    },  function(err) {
            alert("Error occurred " + err.message);
        }
    );   
}

// Transaction error callback
//    
function FalloSincronizacion(tx, err) {
    alert("Error actualizando los clientes: " + err);
}

// Transaction success callback
//
function OkSincronizacion() {

}

Answers


INVALID_STATE_ERR is not an SQLite error.

I'd guess that your OData callback function is run asynchronously, after SincronizarClientes() has returned, so the transaction tx is already closed.

Call db.transaction() inside the callback function instead.


Need Your Help

Custom, Imageless UIButton title disappears

iphone ios objective-c xcode uibutton

I have created in the XIB a custom UIButton with no image. When the button is clicked, the title disappears.

nginx - Disable http to https redirect?

ssl nginx

I followed this tutorial http://www.schenkels.nl/2014/12/reverse-proxy-with-odoo-8-nginx-ubuntu-14-04-lts/ how to make reverse proxy for odoo with nginx.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.