开发者

Can you return an SQLResultSet with JavaScript from an SQLite database?

I'm trying to create a little plugin that will make working with SQLite databases a little easier. I've got the basic CRUD functions except for selecting data from the database.

I would like to return the selected data from the executeSql function, but it seems all I can do is call another method and not just return the data.

So what I'd like to do is:

var data = $(window).dbPlugin( 'select', '*', 'tableName', 'conditions' );

Then I would like to work with the "data" variable as the SQLResultSet from the query. However, the transaction seems to only allow me to call a data handler method and not just return the result.

testDB.transaction(  
    function ( transaction ) {  
        transaction.executeSql( sql, [], dataHandler, errorH开发者_如何学运维andler);  
    }  
);

function dataHandler( transaction, results ) {
    // Now I can work with the "results" as the SQLResultSet
}

Any ideas how to just return the selected data or if it's even possible?


SQLlite is asynchronous, and there isn't any way to avoid that. You could pass in the function you want to be called as your callback into your .dbPlugin method. After conditions pass another value which is a function to be called after the query is run. Then set data in your callback function.

It's pretty annoying, I recently wrote something like you are writing on my last project. I used a jQuery plugin called blockUI that would essentially block the interface during SQL calls which made it appear a little more 'synchronous'

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜