【问题标题】:Cordova SQLite - get results from a sequence of SELECT queriesCordova SQLite - 从一系列 SELECT 查询中获取结果
【发布时间】:2023-03-25 00:28:01
【问题描述】:

我有一个使用 SQLite 数据库的 Cordova 移动应用程序。我正在使用cordova-sqlite-storage 插件。我需要一个接一个地执行两个选择并获得结果。是否可以避免嵌套?如果是这样,怎么办?找不到任何有关如何执行此操作的示例。

附:我需要使用两个查询的结果退出函数

ctx.transaction(function(tx) {
    tx.executeSql("SELECT * FROM cards WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
        // get the result here from first select
        tx.executeSql("SELECT * FROM events WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
            // get the result from second select and return
            // return with results from both queries
        }, function(tx, error) {
            // fail
        });
    }, function(tx, error) {
        // fail
    });
});

【问题讨论】:

    标签: android sqlite cordova


    【解决方案1】:

    由于你不需要第一次选择的结果,我认为你不需要嵌套。这样的事情应该可以工作:

    var result1, result2; 
    ctx.transaction(function(tx) {
        tx.executeSql("SELECT * FROM cards WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
            // get the result here from first select
            result1 = ...;
        }, function(tx, error) {
            // fail
        });
        tx.executeSql("SELECT * FROM events WHERE lastupdate > ?;", [lastSync], function(tx, rs) {
            // get the result from second select and return
            result2 = ...;
        }, function(tx, error) {
            // fail
        });
    }, null, function() {
        //do something with results;
    });
    

    插件仍应按顺序排队并执行它们。

    【讨论】:

    • ops,我很抱歉.. 我确实需要退出函数并得到两个结果.. 我编辑了问题
    • 在这种情况下,我建议向事务添加成功处理程序。如果你的函数有回调或承诺,你可以用那里的结果解决它
    • 我将成功处理程序添加到我的答案中
    • "null" 是错误函数(这里不需要)。对吗?
    • 没错。我不知道为什么成功和错误处理程序的顺序不同。在大多数情况下,插件只是遵循定义它的 websql 规范。 w3.org/TR/webdatabase
    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多