Cordova-sqlite-storage: How to store data in sqlite cordova with foreign key

Created on 23 Dec 2016  路  4Comments  路  Source: storesafe/cordova-sqlite-storage

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]);
            }

        }

In the following image, point out the results of operations with rectangles

explicacion

  • The red color is the record that is inserted of the person, bone the person object.

  • The green color, shows that, it shows that inserted in three the three register, always starting from position 0.

  • The sky blue, the number of rows affected by the query, knowing that I put as a criterion * that specifies all, and therefore I throw 1, which is the number of records.

  • Lastly a purple color, which shows a single record in the Telephone tab, which is supposed to hold 3.

question info needed angular

All 4 comments

For help please post a complete, self contained test program that demonstrates your issue.

@brodybits sorry, i solved.

Thanks!

Hi @PterPmnta,

I have used following lines of code for create two tables with foreign key,

CREATE TABLE artist(artistid  INTEGER PRIMARY KEY, artistname  TEXT);
CREATE TABLE IF NOT EXISTS track(trackid  INTEGER PRIMARY KEY,trackname TEXT,trackartist INTEGER,FOREIGN KEY(trackartist) REFERENCES artist(artistid));

But foreign key(artistid) not stored in track table.

I has try the following code to enable.
query('PRAGMA foreign_keys = ON;');
But no effect, could you please help me to solve this.

Was this page helpful?
0 / 5 - 0 ratings