Javascript Sqlite:检查数据库是否存在

Javascript Sqlite:检查数据库是否存在,javascript,sqlite,cordova,Javascript,Sqlite,Cordova,我正在使用phonegap开发一个移动应用程序,它将一些数据存储到本地数据库sqlite DB中。 我需要知道数据库是否存在,并确定需要执行哪个进程 var database = window.openDatabase("my_db", "1.0", "sample DB", 30000); if (check_db_exist()) { process_1(); } else { process_2(); } 确定数据库是否存在的最佳方法是检查表示它的文件是否存在。这是一个简

我正在使用phonegap开发一个移动应用程序,它将一些数据存储到本地数据库sqlite DB中。 我需要知道数据库是否存在,并确定需要执行哪个进程

var database = window.openDatabase("my_db", "1.0", "sample DB", 30000);
if (check_db_exist()) {
    process_1();
}
else
{
    process_2();
}

确定数据库是否存在的最佳方法是检查表示它的文件是否存在。这是一个简单的IO操作,如以下示例所示:

字符串路径=path.CombineApplicationData.Current.LocalFolder.path,数据库名称

如果文件.Existspath { //你的代码在这里
}

我认为您不能直接检查数据库的存在。我已经研究过了,但还没有找到一种使用简单函数调用的方法。然而,这似乎是一个流行的请求,这里有一个解决方法:

var db = window.openDatabase("my.db", "1", "Demo", -1);


db.transaction(function (tx) {

/*I don't think that there is a way to check if a database exists.
    As I think that the openDatabase call above will just create
    a missing DB. Here is a second-best way to do it - there 
    is a way to check to see if a table exists.  Just pick a table that 
    your DB should have and query it.  If you get an ERROR, then you 
    can assume your DB is missing and you will need to create it. */

console.log("Trying to get the test data");
tx.executeSql("select * from test_table", [], function (tx, res) {

    var len = res.rows.length, i;
    for (i = 0; i < len; i++) {
        console.log(res.rows.item(i).data);
    }
}, function (err) {
    console.log("Error selecting: " + err);

    //NOW we need to create the DB and populate it
    tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text)');

    tx.executeSql('INSERT INTO test_table (data) VALUES("test data one")');
    tx.executeSql('INSERT INTO test_table (data) VALUES("test data two")');

    //now test select
    tx.executeSql("select * from test_table", [], function (tx, res) {

        var len = res.rows.length, i;
        for (i = 0; i < len; i++) {
            console.log(res.rows.item(i).data);
        }
    });


    //now clean up so we can test this again
    tx.executeSql('DROP TABLE IF EXISTS test_table', [], function (tx, res) {
        console.log("dropped table");
    }, function (err) {
        console.log("Error dropping table: " + err);
    });

});
});

如您所见,第一次查询期间的error函数将创建并填充数据库。它对程序逻辑流使用异常处理,但它可以工作

我需要做一些类似的事情,我需要检查应用程序是否已经创建了遗留数据库,如果已经创建了遗留数据库,则将所有数据导出到新数据库new and improved db,然后删除此数据库

背景信息:我正在从简单的键值表转移到带有级联等的复杂关系数据库

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

当然可以,但是您询问如何检查数据库是否存在。因此,您必须检查该文件是否存在。你们在JS中并没有这样做的方法吗?它正在工作,但当我从设备中清除堆栈时,我的数据库被删除了。有什么解决办法吗that@tyler_mitchell知道档案现在在哪里吗?在我的设备上找不到它们。我每x次存档我的dbs;这些r位于这里:var dbPath=cordova.file.applicationStorageDirectory+'databases/name.db';如果是设备!=null&&device.platform=='iOS'dbPath=cordova.file.documentsDirectory+'name.db';
// Fail Method
function setupDB() {
    newDB = window.sqlitePlugin.openDatabase({ name: "<NewImprovedDBName>.db" });
    newDB.transaction(sql.initDB, sql.errorCallback, sql.successCallBack);
}
// Success Method
function exportToNewDB() {
    db = window.sqlitePlugin.openDatabase({ name: "<LegacyDBName>.db" });
    db.transaction(function (tx) {
         setupDB();
         // Insert Export code Here

         // Delete DB
         window.sqlitePlugin.deleteDatabase("<LegacyDBName>.db", sqlSuccess, sqlFail);
    }, sqlFail);
}
window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory + "/databases/my_db.db", process_1, process_2);