htmlweb-sql

WebSQL Autoincrement not working


I'm introducing mysqlf into WebSQL (yeah, I know it is deprecated), and I have run into a trouble...

I create a database through Js as follows:

function initDb(){

try {
    if (!window.openDatabase) {
        alert('Databases are not supported in this browser.');
    } else {

        var db = getDb();

        db.transaction(function (tx){
            tx.executeSql("CREATE TABLE IF NOT EXISTS tipofiesta (id INTEGER AUTO_INCREMENT, honor TEXT, descripcion_en TEXT, descripcion_es TEXT, descripcion_eu TEXT, PRIMARY KEY (id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS localizacion (id INTEGER AUTO_INCREMENT, latitud TEXT, longitud TEXT, PRIMARY KEY (id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS claseacto (id INTEGER AUTO_INCREMENT, nombre_en TEXT, nombre_es TEXT, nombre_eu TEXT, descripcion_en TEXT, descripcion_es TEXT, descripcion_eu TEXT, CONSTRAINT id_pk PRIMARY KEY (id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS AdministradorFiestas (id INTEGER AUTO_INCREMENT, nombreUsuario TEXT, pass TEXT, email TEXT, verificado BOOLEAN DEFAULT FALSE, CONSTRAINT id_pk PRIMARY KEY (id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS provincia (id INTEGER AUTO_INCREMENT, nombreCas varchar(25) DEFAULT 'nombre de provincia', nombreEus varchar(25) DEFAULT 'probintzia izena', id_localizacion INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_localizacion) REFERENCES  localizacion(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS pueblo (id INTEGER AUTO_INCREMENT, nombreCas TEXT, nombreEus TEXT, id_localizacion INTEGER, id_provincia INTEGER,  CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_localizacion) REFERENCES localizacion(id), FOREIGN KEY (id_provincia) REFERENCES provincia(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS fiesta (id INTEGER AUTO_INCREMENT, nombre TEXT, descripcion_eu TEXT, descripcion_en TEXT, descripcion_es TEXT, id_pueblo INTEGER, id_tipofiesta INTEGER, id_fechafiestaanyo INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_pueblo) REFERENCES pueblo(id), FOREIGN KEY (id_tipofiesta) REFERENCES tipofiesta(id), FOREIGN KEY (id_fechafiestaanyo) REFERENCES fechafiestaanyo(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS fechafiestaanyo (id INTEGER AUTO_INCREMENT, fechaInicio date, fechaFin date, id_fiesta INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_fiesta) REFERENCES fiesta(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS diafiesta (id INTEGER AUTO_INCREMENT, fecha date, nombre TEXT, descripcion_en TEXT, descripcion_es TEXT, descripcion_eu TEXT, id_fechafiestaanyo INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_fechafiestaanyo) REFERENCES fechafiestaanyo(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS acto (id INTEGER AUTO_INCREMENT, nombre_en TEXT, nombre_es TEXT, nombre_eu TEXT, descripcion_en TEXT, descripcion_es TEXT, descripcion_eu TEXT, horaInicio time, horaFinAprox time, id_localizacion INTEGER, id_diafiesta INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_localizacion) REFERENCES localizacion(id), FOREIGN KEY (id_diafiesta) REFERENCES diafiesta(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS actoclases (id INTEGER AUTO_INCREMENT, id_acto INTEGER, id_claseacto INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_acto) REFERENCES acto(id), FOREIGN KEY (id_claseacto) REFERENCES claseacto(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS AdministradorFiestasPueblo (id INTEGER AUTO_INCREMENT, idAdministradorFiestas INTEGER, idPueblo INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (idAdministradorFiestas) REFERENCES AdministradorFiestas(id), FOREIGN KEY (idPueblo) REFERENCES pueblo(id))", []);
            tx.executeSql("CREATE TABLE IF NOT EXISTS tipofiestadefiesta (id INTEGER AUTO_INCREMENT, id_tipofiesta INTEGER, id_fiesta INTEGER, CONSTRAINT id_pk PRIMARY KEY (id), FOREIGN KEY (id_tipofiesta) REFERENCES tipofiesta(id), FOREIGN KEY (id_fiesta) REFERENCES fiesta(id))", []);
        });

    }
    return;
} catch(e) {

    if (e == 2) {
        // Version number mismatch.
        console.log("Invalid database version.");
    } else {
        console.log("Unknown error "+e+".");
    }
    return;
}

}

The big problem is that the "id" colums of every table doesn't work with the auto_increment clause... Can I make it work?

Otherwise, I am making a function which tells me the following id available in a table (in order to add data and so on):

function nextIdInTableAvailable(tableName){

try {
    console.log("entramos");

    var idNotAvailable = false;
    var anId = 0;
    console.log("entramos al while...");

    var db = getDb();

    while(idNotAvailable==false){

        console.log("toca vuelta " + anId);
        var currentIdUsed = false;

        db.transaction(function (tx){

                tx.executeSql('SELECT * FROM ' + tableName + ' WHERE id=? ORDER BY id ASC', [anId], function (tx, results) {
                    console.log("hay resultados en la vuelta " + anId);
                    currentIdUsed = true;
                });

        });

        if(!currentIdUsed){
            idNotAvailable = true;
            console.log("vamos a salir en la vuelta " + anId);
        }else{
            anId = anId + 1;
            console.log("seguimos en vuelta siguiente: " + anId);
        }

    }


    console.log("El siguiente id disponible para la tabla '" + tableName + "' es: " + anId );

    return anId;

} catch(e) {

    if (e == 2) {
        // Version number mismatch.
        console.log("Invalid database version.");
    } else {
        console.log("Unknown error "+e+".");
    }

    return 0;

}

}

But, again, a problem: transactions run asynchronously, so the returned value isn't desired...

If auto_increment can't be working, is there any solution to this function?


Solution

  • As transactions run asynchronously, you cannot return in an easy way values...

    Here https://stackoverflow.com/a/12462907/828551 @apsillers has the solution: callback.