如何在phonegap中打开现有数据库

时间:2015-03-24 20:00:56

标签: javascript database cordova phonegap-plugins

我想在与.js文件相同的文件夹中打开名为qw.db的数据库 但是,似乎它创建了一个新文件qw而不是打开我自己的db文件 这是我的代码

function onDeviceReady() {

        var shortName = 'qw';
        var version = '1.0';
        var displayName = 'qwq';
        var maxSize = 65535;
        if (!window.openDatabase) {
            alert('!! Databases are not supported in this Device !! \n\n We are sorry for the inconvenience and are currently working on a version that will work on your phone');
        }
        db = openDatabase(shortName, version, displayName, maxSize);
        createAllTables(db);


    }


    function createAllTables(db){
        db.transaction(function (transaction) {
            transaction.executeSql("CREATE TABLE IF NOT EXISTS model(ModelId INTEGER PRIMARY KEY AUTOINCREMENT,ModelNumber varchr(50))");
        });
        db.transaction(function(transaction){
            var rowCount = 'SELECT * FROM model';
            transaction.executeSql(rowCount,[],function(transaction,result){
                if(result.rows.length == 0){
                    var sqlString = 'INSERT INTO model (ModelId,ModelNumber) VALUES("200","Female")';
                    transaction.executeSql(sqlString);

                }
            });
        });
    }

这是一个参考,如果需要Phonegap Offline Database

1 个答案:

答案 0 :(得分:0)

对于现有数据库,您必须将db文件复制到文档目录或应用程序目录中。您可以使用此cordova-plugin-dbcopy将SQLite数据库从www复制到App Directory