我想返回数据库中特定表的行数WebSQL
里面一个javascript
功能。下面是我的代码。
function getCustomerCount(){
var count = 0;
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
count = results.rows.length;
});
});
return count;
}
我是新来的WebSQL
并且比较陌生javascript
also.
有什么建议么?
你不能这样做:
function getCustomerCount(){
var count = 0;
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
// this function is called when the executeSql is ended
count = results.rows.length;
});
});
// This returns always 0!
return count;
}
您必须像这样使用回调:
function getCustomerCount( callback ){
var count = 0;
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
// this function is called when the executeSql is ended
count = results.rows.length;
callback( count ); // <-- call the callback when is done
});
});
}
并像这样使用它:
getCustomerCount( function( count ) {
// do what you want with the count
console.log( 'The number of rows is: '+ count );
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)