How to store data in sqlite cordova in tables with foreign key


#1

Trying to store information in a database with two tables, but relating the tables with a foreign key, but I have not been able to insert the information.

The code is as follows:

insertarTablas: function(db) {

            var datosPersona, datosTelefono, insertarPersona, insertarTelefono, largoPer, largoTel, i, j;

            datosPersona = [{
                cedula: 123456,
                nombre: "pepe",
                apellido: "perez"
            }];

            datosTelefono = [{
                numero: 30020025,
                cedula: 123456
            }, {
                numero: 6998877,
                cedula: 123456
            }, {
                numero: 58965475,
                cedula: 123456
            }];

            insertarPersona = "INSERT INTO Persona(cedula, nombre, apellido) VALUES(?,?,?)";
            insertarTelefono = "INSERT INTO Telefono(numero, cedula_tel) VALUES(?,?)";

            largoPer = datosPersona.length;
            largoTel = datosTelefono.length;

            for (i = 0; i < largoPer; i++) {
                $cordovaSQLite.execute(db, insertarPersona, [datosPersona[i].cedula, datosPersona[i].nombre, datosPersona[i].apellido]);
            }

            for (i = 0; i < largoTel; i++) {
                $cordovaSQLite.execute(db, insertarTelefono, [datosTelefono[i].numero, datosTelefono[i].cedula]);
            }

        }