为什么无法显示已存储在WebSQL中的数据?

时间:2021-08-08 19:40:04

What's wrong with my function? function getCart can't display data stored in WebSQL. But my function addToCart works and can store data to WebSQL. Please help me if you know my false.

我的功能出了什么问题?函数getCart无法显示存储在WebSQL中的数据。但我的函数addToCart工作并可以将数据存储到WebSQL。如果你知道我的错误,请帮助我。

angular.module('login').factory('CartService',
    ['$webSql', '$http', '$q', 'urls',
        function ($webSql, $http, $q, urls) {
            var factory = {
                initdb: initdb, 
                addToCart: addToCart,
                getCart: getCart
            };

            var db = null;

            return factory

            function initdb(){
                db = openDatabase('CART', '1.0', 'Test DB', 2 * 1024 * 1024);
                db.transaction(function(transaction){
                    transaction.executeSql("CREATE TABLE IF NOT EXISTS cart (id INTEGER PRIMARY KEY, name TEXT, price DOUBLE)");
                });
            }

            function addToCart(name, price){
                    var item = {
                            "name" : name,
                            "price" : price
                    }
                    console.log('Success add cart');

                    db.transaction(function(transaction) {
                        transaction.executeSql("INSERT INTO cart (name, price) VALUES (?, ?)", [name, price]);
                           getCart();
                        });
                }

            function getCart(name){
                var item = {
                        "name" : name
                }

                db.transaction(function(transaction) {
                    transaction.executeSql("SELECT * FROM cart name = ?", [name]);
                    });
            }
    }
    ]);

1 个解决方案

#1


0  

You need to implement callback handler for executeSql (3rd parameter after [name]) to see the fetched records from websql db.

您需要为executeSql([name]之后的第3个参数)实现回调处理程序,以查看来自websql db的已获取记录。

Try this

尝试这个

db.transaction(function(transaction) {
    transaction.executeSql("SELECT * FROM cart name = ?", [name], function(tx, rs) {
        var resultSet =  rs.rows;
        for ( var counter = 0; counter < resultSet.length; counter++ )
        {
            console.log( resultSet.item(counter) );
        }
    });
});

#1


0  

You need to implement callback handler for executeSql (3rd parameter after [name]) to see the fetched records from websql db.

您需要为executeSql([name]之后的第3个参数)实现回调处理程序,以查看来自websql db的已获取记录。

Try this

尝试这个

db.transaction(function(transaction) {
    transaction.executeSql("SELECT * FROM cart name = ?", [name], function(tx, rs) {
        var resultSet =  rs.rows;
        for ( var counter = 0; counter < resultSet.length; counter++ )
        {
            console.log( resultSet.item(counter) );
        }
    });
});