Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

indexeddb - Why is onupgradeneeded never called in this code?

I'm trying to insert data in my indexed db, but the function onupgradeneed never is called, just onsuccess. The version of the database is always the same (it is a global variable). I have understood that in order to get into onupgradeneed the version have to be always the same. In my case, it is in this way. So, I have no data... What can I do? Thanks!

var db;
var dbname = "mi";
var dbversion = 1;

function abrirDB()
{
    if (plataforma == plataformaW8) {
        if (db == null) {
            var request = window.indexedDB.open(dbname, dbversion);
            request.onsuccess = function (evt) {
                db = evt.target.result;
                if (db.objectStoreNames.length == 0)
                    crearDB();
            };
            }
        }
}
function crearDB() {


   //Declaración datos BBDD

        var paisesData = [{ id_pais: 1, pais: "Arabia Saudí", continente: "Asia", capital: "Riad", paisurl: "arabiasaudi", continenteurl: "asia" },
      { id_pais: 2, pais: "Emiratos árabes Unidos", continente: "Asia", capital: "Abu Dhabi", paisurl: "emiratosarabes", continenteurl: "asia" }];


    //////////

    //Abrir BBDD
    var request =window.indexedDB.open(dbname, dbversion);

    request.onerror = function (evt) {
        console.log("Error al abrir la bbdd" + evt.target.errorCode);
    };


    request.onupgradeneeded = function (evt) {
       db = evt.target.result;

        var storePaises = db.createObjectStore(storePaisesNombre, { keyPath: "id_pais", autoincrement: false, unique: true });
      .......
    request.onsuccess = function (evt) {
        //db = request.result;
        db = evt.target.result;
    };

  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your dbversion variable to 2. This way the db will open in a newer version and the onupgradeneeded callback will get called


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...