我的功能出了什么问题?函数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]);
});
}
}
]);
答案 0 :(得分:0)
您需要为executeSql
([name]
之后的第三个参数)实现回调处理程序,以查看来自 websql db 的已获取记录。
试试这个
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) );
}
});
});