JavaScript - No me importa todos los registro del fichero a sqlite

 
Vista:
Imágen de perfil de Yoel
Val: 18
Ha aumentado 1 puesto en JavaScript (en relación al último mes)
Gráfica de JavaScript

No me importa todos los registro del fichero a sqlite

Publicado por Yoel (6 intervenciones) el 13/11/2019 01:10:08
Tengo la siguiente situación, estoy haciendo una importación de un fichero .csv con $.ajax() a una base de datos websql en Phongap (cordova). Lo que me está sucediendo, es que a pesar que estoy definiendo que la función $.ajax() sea sincrónica nunca me importa igual el número de registro, a mi tabla de la base de datos, el cual puede variar en cada ejecución desde 50, 300, o el total que son 726.

A continuación les dejo el código para ver si me pueden ayudar.

Gracias.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var db = window.openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
db.transaction(function(tx) {
    //tx.executeSql('DROP TABLE IF EXISTS `inventario`');
    tx.executeSql('DELETE FROM `inventario`');
    tx.executeSql('CREATE TABLE IF NOT EXISTS `inventario` (`codigo`, `nombre`, `inventario`, `imagen`, `volumenoz`,`volumenml`,`proveedor`,`linea`,`tipo`,`cajaalmacen`,`preciodecompra`,`preciodeventamxn`,`colores`,`codigodebarras`,`unidadescompradas`,`ordendecompra`,`unidadesvendidas`, `ordendeventa`)');
    /*tx.executeSql('CREATE TABLE IF NOT EXISTS `proveedor` (`nombre`,  `contacto`, `ubicacion`,`email`,    `telefonoFijo`, `telefonoMovil`,`direccion`,`productos`,`linea`,`tipo`, `ordendeCompra`,`otalOrders`,`comentarios`, `facturas`,`facturas2`)');
    tx.executeSql('CREATE TABLE IF NOT EXISTS `ubicacion` (`numerodecaja`,`inventariodelProducto`,`numerodeProductos`,`peso`,`palet`,`nota`,`field7`)');
    tx.executeSql('CREATE TABLE IF NOT EXISTS `ordenventa` (`Date`,`Product`,`SalePlatform`,`Price`,`Quantity`,`Revenue`)');
    tx.executeSql('CREATE TABLE IF NOT EXISTS `orden_compra` (`name*`,`status`,`product`,`quantity`,`arriveBy`,`unitsArrived`,`image`,`invoice`,`paid`,`cajaProvedor`,`inventariodelProducto`)');*/
    msg = '<p>Log message created and row inserted.</p>';
    console.log(msg);
});
//Verificamos si la tabla tiene datos para sino insertarlos.
db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM `inventario`', [], function(tx, results) {
        var len = results.rows.length;
        if (len > 0) {
            return true;
            console.log("La tabla está llena.");
        } else {
            $.ajax({
                url: 'files/tbinventario.csv',
                dataType: 'text',
                async: false,
                beforeSend: function() {
                    $("#idmsgImp").html('<div class="alert alert-danger" role="alert">Importando datos inventario</div>');
                    $('#importar').attr("disabled", 'disabled');
                },
                success: function(data) {
                    var allRows = data.split(/\r?\n|\r/);
                    var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
                    let dataVal;
                    let aVal;
                    $.each(allRows, function(index, value) {
                        if (index > 0) {
                            delete dataCamp;
                            let dataCamp = '`codigo`, `nombre`, `inventario`, `imagen`, `volumenoz`,`volumenml`,`proveedor`,`linea`,`tipo`,`cajaalmacen`,`preciodecompra`,`preciodeventamxn`,`colores`,`codigodebarras`,`unidadescompradas`,`ordendecompra`,`unidadesvendidas`, `ordendeventa`';
                            //Combertimos el string a array 
                            aVal = value.split(',');
                            delete dataVal;
                            let dataVal = '"' + aVal[0] + '","' + aVal[1] + '","' + aVal[2] + '","' + aVal[3] + '","' + aVal[4] + '","' + aVal[5] + '","' + aVal[6] + '","' + aVal[7] + '","' + aVal[8] + '","' + aVal[9] + '","' + aVal[10] + '","' + aVal[11] + '","' + aVal[12] + '","' + aVal[13] + '","' + aVal[14] + '","' + aVal[15] + '","' + aVal[16] + '","' + aVal[17] + '"';
                            db.transaction(function(tx) {
                                tx.executeSql('INSERT INTO `inventario` (' + dataCamp + ') VALUES (' + dataVal + ')');
                            });
                        }
                    });
                    setInterval(function() {
                        $('#importar').attr("enable", 'enable');
                        $("#idmsgImp").html('<div class="alert alert-danger" role="alert">Los datos de inventario ha sido importado</div>');
                    }, 50000);
                },
                error: function() {
                    $("#idmsgImp").html('<div class="alert alert-danger" role="alert">Error en el proceso de los datos</div>');
                }
            });
        }
    }, null);
})
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder