dimanche 8 novembre 2015

cordova sqlite: insert many to many relation

When i have the following json for example:

var book = {
    title: "title",
    publisher: "publisher",
    author: 
    [
        {
            name: "author 1"
        },
        {
            name: "author 2"
        }
    ]
};

How can i insert it into my database using cordova-sqlite-plugin and transaction so it rollbacks everything on error.

I started with the following:

function insertBook(book) {
    database.transaction(queryDB, errorDB, okDB);

    function queryDB(insert_book, [book.title, book.publisher], 
        function (transaction, result) {
            insertId = result.insertId;
        });
    }

    function errorDB(error) {
        // some error appeared
    }

    function okDB () {
        // everything ok
    }
}

But i dont know how to continue when i have the insertId. also for each author i inset i will get an id and i have to store the id of the book and the id of the author in my m:n table.

Aucun commentaire:

Enregistrer un commentaire