我们在循环中得到相同的结果。如果有人能理解我在哪里犯错,请告诉我。请看这个截图以获得一个想法。
我尝试提醒许多地方,似乎一切都很顺利。但不知道为什么不能得到结果。做" executeSql"在循环中使用多个值无法正常工作?
或者在选择查询中是否有任何错误?请让我知道为什么,这会造成这样的问题吗?
var db;
var shortName = 'WebSqlDB';
var version = '1.0';
var displayName = 'WebSqlDB';
var maxSize = 65535;
window.onload = function(){
document.addEventListener("deviceready",init,false);
}
function init(){
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
db = openDatabase(shortName, version, displayName,maxSize);
db.transaction(function(tx){
tx.executeSql( 'DROP TABLE post_details',nullHandler,nullHandler);
tx.executeSql( 'CREATE TABLE IF NOT EXISTS post_details(UserId INTEGER NOT NULL PRIMARY KEY, post_title TEXT NOT NULL, post_date TEXT NOT NULL,post_comment TEXT NOT NULL,post_content TEXT NOT NULL, post_categories TEXT NOT NULL, post_image TEXT)',[],nullHandler,errorHandler);
},errorHandler,successCallBack);
if(checkConnection() != "No network connection"){
$.ajax({
url: 'http://www.foduu.com/api/get_recent_posts/?callback=?',
type: 'GET',
dataType: 'json',
success: function(data){
console.log(data);
var number_of_post = Object.keys(data.posts).length;
for(var i=0; i < number_of_post; i++){
var ptitle = data.posts[i].title;
var pdate = data.posts[i].date;
var pccount = data.posts[i].comment_count;
var pcontent = data.posts[i].content;
var pcategories = data.posts[i].categories;
var pimage = "my image";
//alert(ptitle +","+ pdate +","+ pccount+","+ pcategories +","+ pimage);
db.transaction(function(transaction) {
transaction.executeSql('INSERT INTO post_details(post_title,post_date,post_comment,post_content,post_categories,post_image) VALUES (?,?,?,?,?,?)',[ptitle,pdate,pccount,"pcontent",pcategories,pimage],nullHandler,errorHandler);
});
}
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM post_details;', [],
function(transaction, result) {
$.each(result.rows,function(index){
var row = result.rows.item(index);
var post_single="<li class='post'>" +
"<a href='#' class='post_more'></a>" +
"<div class='post_right_reveal'>" +
"<h4><a href='#' onclick='swiperParent.swipeTo(4);'>"+ row['post_title'] +"</a></h4>" +
"</div>" +
"<div class='post_right_unreveal'>" +
"Posted in <a href='#'>blog category</a>" +
"<span class='post_comments'>25 Comments</span>" +
"<a href='#' class='post_readmore' onclick='swiperParent.swipeTo(4);'>read more</a>" +
"</div>" +
"<div class='post_left'>" +
"<span class='day'>7</span>" +
"<span class='month'>feb</span>" +
"</div>" +
"</li>";
$('#posts-all').append(post_single);
$('#posts-all').trigger('create');
});
});
},errorHandler,nullHandler);
},
error: function(data){
alert("Can Not Fetch data From server");
}
});
}
else
{
alert("No Internet Connection");
}
}
function checkConnection() {
var networkState = navigator.network.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.NONE] = 'No network connection';
return states[networkState];
}
function errorHandler(transaction, error) {
alert('Error: ' + error.message + ' code: ' + error.code);
}
function successCallBack() {
alert("DEBUGGING: success");
}
function nullHandler(){};
有没有什么方法可以在所有其他JS或CSS文件之前加载我的main.js文件?
我尝试执行多个executeSql它工作得很好。但是当我在FOR循环中使用它时,它仍然一次又一次地给我相同的标题。如果需要,我可以将代码放在github上供您试用。
提前谢谢!
答案 0 :(得分:0)
好的问题已经解决,我正在db.trasaction(..)
运行FOR循环,它应该在tx.excuteSql(...)
运行,这解决了我的问题。
我很高兴我自己找到了解决方案。 :)
谢谢!