I'm making a phonegap app which reads json data from external server via ajax. I need to load and save data in a sqlite database. Native sqlite generates an callback function error about which I asked on stackoverflow but didn't get any answer. I decided then to use a sqlite plugin but when I use it in my app, error function defined in ajax executes. Without plugin, it works well. Please tell me how I can use sqlite with or without plugin without any error. Here's the code I'm using:
var DB = window.sqlitePlugin.openDatabase({name: "Posts_DB.db"});
var BASE_URI = "http://ift.tt/1PZwZie";
var POST_OFFSET = 0;
function getPosts() {
$.mobile.loading('show');
$.ajax({
url: BASE_URI + "posts",
type: "GET",
dataType: "json",
data: {
"number": 10,
"offset": POST_OFFSET,
"order": "DESC"
},
error: function() {
alert("An error occured.");
},
success: function(response) {
$.each(response.posts, function(index, data) {
$('<li><a href="#single" data-transition="slide" id="'+data.ID+'"><h1>'+data.title+'</h1><p>'+getDays(data.date)+'</p></a></li>').appendTo('#latest-list');
$(document).on("click", "#"+data.ID, function() {
showPost(data.title, data.date, data.content, data.URL);
});
});
$('#latest-list').listview('refresh');
$.mobile.loading("hide");
}
});
POST_OFFSET+=10;
}
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
$(document).ready( function() {
DB.transaction(createTableDB, errorCB, successCB);
getPosts();
});
}
function createTableDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Posts(id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT NOT NULL, Date TEXT NOT NULL, Content TEXT NOT NULL, Link TEXT NOT NULL)');
}
function populateDB(tx) {
var title = document.getElementById('entry-title').innerHTML;
var date = document.getElementById('entry-date').innerHTML;
var content = document.getElementById('entry').innerHTML;
var url = document.getElementById('browser').getAttribute('href');
tx.executeSql('INSERT INTO Posts(Title,Date,Content,Link) VALUES(?,?,?,?)', [title, date, content, url] );
}
function errorCB(err) {
alert("Error processing SQL: "+err.message);
}
function successCB() {
DB.transaction(queryDB, errorCB);
}
function queryDB(tx){
tx.executeSql('SELECT * FROM Posts', [], querySuccess, errorCB);
}
function querySuccess(tx,result){
$('#saved-articles-list').empty();
for( var index = 0; index < result.rows.length; index++) {
var row = result.rows.item(index);
$('#saved-articles-list').append('<li><a id="'+row['id']+'" data-transition="slide" href="#single"><h1 class="ui-li-heading">'+row['Title']+'</h1><p class="ui-li-desc">'+getDays(row['Date'])+'</p></a></li>');
$(document).on("click", row['id'], function() {
showPost(row['Title'], row['Date'], row['Content'], row['Link']);
});
}
$('#saved-articles-list').listview("refresh");
}
Config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://ift.tt/18Fk3Vk"
xmlns:gap = "http://ift.tt/16o90D3"
xmlns:android = "http://ift.tt/nIICcg"
id = "com.smushbits.app"
versionCode = "10"
version = "1.0.0" >
<!-- versionCode is optional and Android only -->
<name>TestApp</name>
<description>
Test app
</description>
<author href="http://ift.tt/1IhuqBF" email="smartvishalsingh@gmail.com">
Vishal Singh
</author>
<access origin="*" />
<platform name="android" >
<preference name="orientation" value="portrait" />
<preference name="fullscreen" value="true" />
</platform>
<feature name="http://ift.tt/NwTf7f" />
<plugin name="com.indigoway.cordova.whitelist.whitelistplugin" source="pgb" />
<plugin name="cordova-plugin-inappbrowser" spec="1.1.0" />
<plugin name="io.litehelpers.cordova.sqlite" spec="0.7.10" source="pgb" />
<plugin name="cordova-plugin-x-socialsharing" spec="5.0.7" />
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="data:*" />
<allow-intent href="*" />
<icon src="icon.png" />
<splash src="splash.png" />
</widget>
Aucun commentaire:
Enregistrer un commentaire