Android phonegap application having issues with SQlite and local storage on Samsung Galaxy devices
I am having an issue with a PhoneGap app that has been built for both Android and iOS. On iOS and most Android devices, it's functioning perfectly, except for Samsung Galaxy S3 and S4.
On the first page load, the app creates a local SQL database. This database is used to store values from questions and answers throughout the app.
The issue I am having on the Samsung devices is that the database will not load properly the first time the app is run, however if a user closes the app completely and reopens it, the database is created with no errors. Because the first page requires a user to enter their age and then stores the value in a SQL database, users are getting the impression that the app has frozen at this point.
Initialization code for database:
index.html
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
populateDB();
}
main_js.js
function populateDB() {
var db = window.openDatabase("Database", "1.0", "My Database", 20000);
db.transaction(populate_DB, errorCB, successCB);
}
function populate_DB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Options (ID INTEGER UNIQUE NOT NULL, Name TEXT NOT NULL, Value INTEGER NOT NULL)');
tx.executeSql('CREATE TABLE IF NOT EXISTS Questions (ID INTEGER UNIQUE NOT NULL, Question TEXT NOT NULL, Answer TEXT)');
tx.executeSql('INSERT OR IGNORE INTO Options (ID, Name, Value) VALUES (1, "License", 0)');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (0, "Age","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (1, "two","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (2, "three","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (3, "four","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (4, "five","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (5, "six","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (6, "seven","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (7, "eigth","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (8, "nine","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (9, "ten","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (10, "eleven","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (11, "twelve","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (12, "thirteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (13, "fourteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (14, "fifteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (15, "sixteen","")');
tx.executeSql('INSERT OR IGNORE INTO Questions (ID, Question, Answer) VALUES (16, "seventeen","")');
}
age_button_pressed
function age_save() {
var db = window.openDatabase("Database", "1.0", "My Database", 20000);
loc = "q_sex.html";
db.transaction(age_query, errorCB);
}
function age_query(tx) {
var age = document.getElementById("a_age").value;
tx.executeSql('UPDATE Questions SET Answer = "' + age + '" WHERE ID="0";', [], q_querySuccess, errorCB);
}
After age_button_pressed
, there is nothing that happens and the database does not update the result.
It is only a problem on the Samsung Galaxy S3 and S4, and I am wondering if Samsung's WebKit has something to do with it?
Solution 1:
It might be because you are opening the database twice. Only open it once and do all of your transactions on that single instance. Also make sure that your previous transaction has finished before starting another one. You shouldn't have to worry about that, but in my experience Cordova's (PhoneGap) WebSQL is a bit finicky.
Solution 2:
I used phonegap until 2.0 version to 3.1 for samsung and htc devices and never seen such a problem. Which version are you using? Try to use stable versions, not beta.
First of all, sometimes phonegap apps can have sync problem on startup. Maybe that issue can be related with that. You can try that just put deviceready in index.html and after deviceready, direct your page to your main page. In main page do your database operations(like creating tables and inserting).
Second, you need to trace error messages on errorCB function. error function gets a parameter and that parameter includes errorcode and errormessage. Check these out.
And last, if you are using s4 device with android version 4.4, you can debug your javascript codes with chrome. Debugging is a big advantage for phonegap. Use it.