mercredi 11 février 2015

Phonegap App with SQLLIte works properly on Android Emulator but not on Android Device

i am developing an app with phonegap with sqllite plugin. my app have two html files and two javascript files. app is properly runs on android emulator but not runs on android dCould not prepare statement". the above error is coming from following code.



//add listener when device ready
document.addEventListener("deviceready", onDeviceReady, false);
db = window.openDatabase("railway", "1.0", "Railway Ticket", 200000); //will create database Dummy_DB or open it

//function will be called when device ready
function onDeviceReady(){
db.transaction(populateDB, errorCB, loadStn);
}

//create table and insert some record
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS farechart (id INTEGER PRIMARY KEY AUTOINCREMENT, fromkm TEXT NOT NULL, tokm TEXT NOT NULL, scrs TEXT NOT NULL, fcrs TEXT NOT NULL)');
tx.executeSql('CREATE TABLE IF NOT EXISTS stationdst (id INTEGER PRIMARY KEY AUTOINCREMENT, stname TEXT NOT NULL, stkm TEXT NOT NULL, stline TEXT NOT NULL)');
tx.executeSql('INSERT INTO farechart(fromkm,tokm,scrs,fcrs) VALUES ("1", "5", "5", "45"),("6", "10", "5", "45"),("11", "15", "5", "60"),("16", "20", "5", "95"),("21", "25", "10", "95"),("26", "30", "10", "125"),("31", "35", "10", "125"),("36", "40", "10", "135"),("41", "45", "10", "135"),("46", "50", "15", "145"),("51", "55", "15", "150"),("56", "60", "15", "155"),("61", "65", "15", "155"),("66", "70", "15", "170"),("71", "75", "20", "180"),("76", "80", "20", "180"),("81", "85", "25", "195"),("86", "90", "25", "195"),("91", "95", "25", "195"),("96", "100", "25", "210")');
tx.executeSql('INSERT INTO stationdst(stname,stkm,stline) VALUES ("Churchgate", "0", "W"),("Marine Lines", "2", "W"),("Charni Road", "3", "W"),("Grant Road", "4", "W"),("Mumbai Central", "5", "W"),("Mahalaxmi", "6", "W"),("Lower Parel", "8", "W"),("Elphinstone Road", "9", "W"),("Dadar", "11", "W"),("Matunga Road", "11.5", "W"),("Mahim", "15", "W"),("Bandra", "15", "W"),("Khar Road", "17", "W"),("Santacruz", "18", "W"),("Vile Parle", "20", "W"),("Andheri", "22", "W"),("Jogeshwari", "24", "W"),("Goregaon", "27", "W"),("Malad", "30", "W"),("Kandivali", "32", "W"),("Borivali", "34", "W"),("Dahisar", "37", "W"),("Mira Road", "40", "W"),("Bhayandar", "44", "W"),("Naigaon", "48", "W"),("Vasai Road", "52", "W"),("Nala Sopara", "56", "W"),("Virar", "60", "W"),("CST", "0", "C"),("Masjid Bunder", "2", "C"),("Sandhurst Road", "3", "C"),("Byculla", "5", "C"),("Chinchpokli", "6", "C"),("Currey Road", "7", "C"),("Parel", "8", "C"),("Dadar", "9", "C"),("Matunga", "11", "C"),("Sion", "13", "C"),("Kurla", "16", "C"),("Vidyavihar", "18", "C"),("Ghatkopar", "20", "C"),("Vikhroli", "23", "C"),("Kanjurmarg", "25", "C"),("Bhandup", "26", "C"),("Nahur", "28", "C"),("Mulund", "31", "C"),("Thane", "34", "C"),("Kalwa", "36", "C"),("Mumbra", "40", "C"),("Diwa", "43", "C"),("Kopar", "47", "C"),("Dombivli", "49", "C"),("Thakurli", "50", "C"),("Kalyan", "54", "C")');
}

//function will be called when an error occurred
function errorCB(err) {
alert("Error processing SQL: "+err.code+ " message=" + err.message);
}
function errorCB2(err2) {
alert("Error processing SQL 2: "+err2.code+ " message=" + err2.message);
}
function errorCB3(err3) {
alert("Error processing SQL 3: "+err3.code+ " message=" + err3.message);
}

//function will be called when process succeed
function loadStn() {
//alert("success!");
db.transaction(loadTblStn, errorCB2);
}

//select all from SoccerPlayer
function loadTblStn(tx){
tx.executeSql('SELECT * FROM stationdst', [], stnresult, errorCB3);
}

function stnresult(tx,result){
var nores = result.rows.length;
for (var i=0; i<nores; i++){
var row1 = result.rows.item(i);
$('#source').append('<option value="'+row1['id']+'">'+row1['stname']+'</option>');
$('#destination').append('<option value="'+row1['id']+'">'+row1['stname']+'</option>');
}
//$("#source").selectmenu('refresh', true);
//$("#destination").selectmenu('refresh', true);
}


function startBuy(tx)
{
//alert('in function startBuy');
var source = document.getElementById('source').value;
var desti = document.getElementById('destination').value;
var tclass = document.getElementById('tclass').value;
var adlt = document.getElementById('adults').value;
var chld = document.getElementById('children').value;
var jtype = document.getElementById('journey').value;

//alert("data:"+source+" "+desti+" "+tclass+" "+adlt+" "+chld+" "+jtype);

db.transaction(function (tx){ tx.executeSql('SELECT * FROM stationdst WHERE id=?', [source], sourceInfo, errorCB3); });
db.transaction(function (tx){ tx.executeSql('SELECT * FROM stationdst WHERE id=?', [desti], destInfo, errorCB3); });

}

function sourceInfo(tx,resultS){
var rowS = resultS.rows.item(0);
stkmS = rowS['stkm'];
stlineS = rowS['stline'];
}

function destInfo(tx,resultD){
var rowD = resultD.rows.item(0);
stkmD = rowD['stkm'];
stlineD = rowD['stline'];

calculateFare1();
//alert(""+stkmS+" "+stkmD);
}

function calculateFare1()
{
//alert(""+stkmS+" "+stkmD+" "+stlineS+" "+stlineD);
if(stlineS == stlineD)
{
mainkm = stkmS-stkmD;
mainkm = Math.abs(mainkm);
}
else
{
viakmW = '11'; // via station on western railway
viakmC = '9'; // via station on central railway
if(stlineS == 'W')
{
wstrnkm = stkmS-viakmW;
wstrnkm = Math.abs(wstrnkm);

cntrlkm = stkmD-viakmC;
cntrlkm = Math.abs(cntrlkm);

mainkm = wstrnkm+cntrlkm;
}
else if(stlineS == 'C')
{
wstrnkm = stkmD-viakmW;
wstrnkm = Math.abs(wstrnkm);

cntrlkm = stkmS-viakmC;
cntrlkm = Math.abs(cntrlkm);

mainkm = wstrnkm+cntrlkm;
}
}
//alert(mainkm);
db.transaction(function (tx){ tx.executeSql('SELECT * FROM farechart WHERE fromkm<? and tokm>?', [mainkm,mainkm], calculateFare2, errorCB3); });
}

function calculateFare2(tx,resultF){
var rowF = resultF.rows.item(0);
scrs = rowF['scrs'];
fcrs = rowF['fcrs'];

alert("Second Class Rs."+scrs+" First Class Rs."+fcrs);
}


when the app loads at that time the first js file is executed which is:



//add listener when device ready
document.addEventListener("deviceready", onDeviceReady, false);
db = window.openDatabase("railway", "1.0", "Railway Ticket", 200000); //will create database Dummy_DB or open it

//function will be called when device ready
function onDeviceReady(){
db.transaction(populateDB, errorCB, isLoggedin);
}

//create table and insert some record
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL)');
tx.executeSql('CREATE TABLE IF NOT EXISTS logedinuser (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, password TEXT NOT NULL, remember TEXT NOT NULL)');
tx.executeSql('INSERT INTO users(name,username,password,email) VALUES ("Administrator", "admin", "admin", "admin@gmail.com")');
//tx.executeSql('INSERT INTO logedinuser(username,password,remember) VALUES ("admin", "admin", "1")');
tx.executeSql('DELETE FROM logedinuser');
}

//function will be called when an error occurred
function errorCB(err) {
alert("Error processing SQL: "+err.code+ " message=" + err.message);
}
function errorCB2(err2) {
alert("Error processing SQL 2: "+err2.code+ " message=" + err2.message);
}
function errorCB3(err3) {
alert("Error processing SQL 3: "+err3.code+ " message=" + err3.message);
}

//function will be called when process succeed
function isLoggedin() {
//alert("success!");
db.transaction(chklogin, errorCB2);
}

//select all from SoccerPlayer
function chklogin(tx){
tx.executeSql('SELECT * FROM logedinuser', [], lguserresult, errorCB3);
}

function lguserresult(tx,result){
var nores = result.rows.length;
if(nores <= '0')
{
alert('Please Login or Create new account!');
}
else
{
//var row1 = result.rows.item(index);
var row1 = result.rows.item(0);

var remember = row1['remember'];
if(remember == '1')
{
window.location.href="index.html";
}
else
{
alert('Please Login or Create new account!');
}
}
}


after that the second js file is executed on different page which throws me an error.


please help..


Aucun commentaire:

Enregistrer un commentaire