如何将JSON对象数据插入SQLite
我有以下代码将JSON对象插入SQLITE数据库。 我使用for循环将每个json对象数据插入数据库。 但最后一个JSON对象只插入到数据库中如何将JSON对象数据插入SQLite,json,sqlite,Json,Sqlite,我有以下代码将JSON对象插入SQLITE数据库。 我使用for循环将每个json对象数据插入数据库。 但最后一个JSON对象只插入到数据库中 db.transaction(function(tx) { tx.executeSql('CREATE TABLE news_posts (id integer primary key, title text, content text,thumbnail text,url text,date datetime)'); }); f
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE news_posts (id integer primary key, title text, content text,thumbnail text,url text,date datetime)');
});
for (var i = 0; i < json.posts.length; i++) {
db = window.openDatabase("news", "1.0", "Posts database", 200000);
db.transaction(function(tx) {
tx.executeSql("INSERT INTO news_posts (title,content,thumbnail,url,date) VALUES (?,?,?,?,?)", [json.posts[i].title,json.posts[i].content,json.posts[i].thumbnail,json.posts[i].url,json.posts[i].date], function(tx, res) {
console.log("Success");
});
});
}
您在控制台中看到多少条成功消息?68json响应大小日志@Googie新闻发布表是如何创建的?你能为它粘贴CREATETABLE语句吗?您可以从sqlite_master中选择sql,其中name='news_posts';我用那个代码更新了帖子@Googie您还应该提供错误处理程序函数,而不仅仅是结果处理程序,因为调用结果处理程序并不意味着它成功。在查询中再添加一个处理函数,它将为您打印错误。然后看看你是否有错误。