2

目前正在使用 node-webkit 创建应用程序。我可以在 chrome 浏览器中看到本地数据库。但是我想从 mozilla sqllite add on 连接数据库。这可能吗。以前数据库是在我的应用程序的用户目录中创建的.我还有一个疑问。对于每次登录,我都会清空也无法正常工作的数据库。

4

1 回答 1

1

在 SQLite 管理器中,您可以“导出数据库”。这将为您提供在 node-webkit 应用程序中作为本地数据库安装所需的所有 SQL 命令。这是我如何处理所有这些命令的代码片段:

        var importDatabase = function(all_sql_commands) {

            var index = 0;
            var deferred = $q.defer();

            nextRecord(index, all_sql_commands);

            function nextRecord(index, all_sql_commands) {

                if (index < all_sql_commands.length) {
                    db.transaction(function(tx) {
                        tx.executeSql(all_sql_commands[index],
                                [],
                                function(transaction, results) {
                                    nextRecord(++index, all_sql_commands);
                                },
                                function(transaction, error) {
                                    console.log(error.message + ". Trying to execute: " + all_sql_commands[index]);
                                    nextRecord(++index, all_sql_commands);
                                });
                    });
                } else {
                    deferred.resolve("all done");
                }
            }
            ;
            return deferred.promise;
        }

我在工厂中有这段代码并且是递归的,因此一次只处理一个命令。这就是我在进入数据库时​​初始化数据库的方式:

configFactory.database().then(function(results) {
    var deferred = $q.defer();
    deferred.resolve(results);
    return deferred.promise;
}).then(function(results) {
    sqlManager.exportTables().then(function(tables) { // get a list of tables
        sqlManager.dropTables(tables).then(function() {// make sure they are clean
            sqlManager.importSqlCommands(results.import_sql_file_name).then(function(all_sql_commands) { // read in importSQL.txt, CREATE TABLE first, then INSERT INTO's next
                sqlManager.importDatabase(all_sql_commands).then(function(results) { // loop through the commands and run the SQL scripts.
                    $scope.databaseInstalled = "Database installed";
                });
            });
        });
    });
}); 

我的配置工厂:

define(['angular', 'sherpaManagerFactories'], function(angular) {

var databaseConfig = window.requireNode('./app/node/config/database');
angular.module('sherpaManager.factories').factory('configFactory', ['$q', function($q) {

        var database = function() {
            var deferred = $q.defer();
            deferred.resolve(databaseConfig.database);
            return deferred.promise;
        };

        return {
            database: database
        };
    }]); 
});

最后是我的 databaseConfig 本身:

var Q = window.requireNode('q');

var deferred = Q.defer();

var database = function() {
    var obj = {};
    obj.database_name = "SherpaManager"; // TODO figure out how to pass this into the sqlManager factory. Right now it's hard coded into the sqlManager
    obj.import_sql_file_name = "./config/SherpaManager.sql";
    deferred.resolve(obj);
    return deferred.promise;
}

module.exports.database = database();

希望这可以帮助

于 2014-02-24T21:05:47.703 回答