Sqlite: check if database exist

后端 未结 3 1021
野的像风
野的像风 2021-01-19 02:47

I am developing a mobile application using phonegap that store some data into the local database (sqlite DB). I need to know if the database exist or not, and that to determ

3条回答
  •  不知归路
    2021-01-19 03:03

    I needed to do something similar, I needed to check if the application had a db already created (legacy DB) and if so export all the data to the new db (new and improved DB) and then delete this DB.

    Background Info: I was moving from simple keyvalue tables to complex relational DB with cascades etc.

    function onDeviceReady() {
        // CHECK IF LEGACY DATABASE EXISTS. IF DOES EXPORT EXISTING DATA TO NEW DB THEN DELETE LEGACY
        window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory + "/databases/.db", exportToNewDB, setupDB);
    }
    

    Note: If file exists (success), then we need to do our export code in here, then delete file so this method will always fail. If file doesn't exist - user has already exported to new DB or they our new user and never had legacy DB.

    // Fail Method
    function setupDB() {
        newDB = window.sqlitePlugin.openDatabase({ name: ".db" });
        newDB.transaction(sql.initDB, sql.errorCallback, sql.successCallBack);
    }
    // Success Method
    function exportToNewDB() {
        db = window.sqlitePlugin.openDatabase({ name: ".db" });
        db.transaction(function (tx) {
             setupDB();
             // Insert Export code Here
    
             // Delete DB
             window.sqlitePlugin.deleteDatabase(".db", sqlSuccess, sqlFail);
        }, sqlFail);
    }
    

    Answer to your Question:

    window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory + "/databases/my_db.db", process_1, process_2);
    

提交回复
热议问题