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

Sep 7, 2012 at 11:07 PM
Edited Sep 7, 2012 at 11:10 PM

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".

 Please help me.

This is the code:

 

function SincronizarClientes()  // This function runs after a button click
{
    var db = window.openDatabase("Teyuna", "1.0", "Meteora - Teyuna", 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() {
    
}
Sep 8, 2012 at 12:32 PM
Edited Sep 8, 2012 at 12:32 PM

The solution: http://stackoverflow.com/questions/12326336/get-data-from-odata-with-datajs-and-save-to-sqlite-in-phonegap?answertab=votes#tab-top

"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."