insert data in sqlite database in phonegap when first time load application - android

i have an application write with phonegap , i want when first time my application load , tables and data create and insert in database , but when run my application every time , insert data done again , and load time increase , how can chek if table created dont insert data again ?
document.addEventListener("deviceready", onDeviceReady, false);
var dbShell ;
function onDeviceReady() {
dbShell = window.openDatabase("BaharNarenj", "1.0", "BaharNarenj", 2000000);
dbShell.transaction(setupTable,dbErrorHandler,getEntries);
}
function setupTable(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS amaken(id INTEGER,title,des,px,py)");
tx.executeSql('insert into amaken(id,title,des,px,py) values(2,"test","dec","36.566185","55.059502")');
tx.executeSql('insert into amaken(id,title,des,px,py) values(4,"test5","dec5","36.566185","55.059502")');
}
function dbErrorHandler(err){
alert("DB Error: "+err.message + "\nCode="+err.code);
}
function getEntries() {
alert("done");
}

You can set one flag (like XApp1.0 in following code) in local storage and check that flag's value while subsequent run of the app. Hope this will help you.
function onDeviceReady() {
var firstrun = window.localStorage.getItem("XApp1.0");
if ( firstrun == null ) {
window.localStorage.setItem("XApp1.0", "1");
var db = window.openDatabase("XApp", "1.0", "XApp", 200000);
db.transaction(populateDB, errorCB, successCB);
}
else {
// Db Alredy Exists
var db = window.openDatabase("XApp", "1.0", "XApp", 200000);
db.transaction(queryDB, errorCB);
}
}

Related

Query executes successfully but still error persists

I am inserting a row in sqlite table in phonegap using following function. My row is successfully inserted but after that it gives me error message
function executeQuery(qry_str){
var db = getDatabase();
var response = db.transaction(function(tx){
tx.executeSql(qry_str);
}, successCB, errorCB);
}
function errorCB(){
console.log('query error');
}
function successCB(){
console.log('success');
}
Please check out the mistake I am doing.

SQLitePlugin with cordova and ANDROID

I need to use 2 sqlite database for my app
the 2 databases have the some structure because one of it is StoricDB
in some procedure i need to use both db.
i do this code:
var onDeviceReady = function ()
{
testwith2db();
};
document.addEventListener("deviceready", onDeviceReady, true);
function testwith2db (){
var dbCurrent_path = "/sdcard/Android/data/myDatabase/DbCurrent"
var dbStoric_poath = "/sdcard/Android/data/myDatabase/DbStoric"
// this work fine
var db_current = window.sqlitePlugin.openDatabase(dbCurrent_path , "1.0", "DbCurrent", 10000000);
db_current.transaction(doquerycurrent, on_tran_error, on_tran_success);
//this work fine
var db_storic = window.sqlitePlugin.openDatabase(dbStoric_poath , "1.0", "DbStoric", 10000000);
db_storic.transaction(doquerystoric, on_tran_error, on_tran_success);
/*
following lines are my problem
because if i do:... another query on currentdb, the query is alwais executed on last db opened
for me is impossible to use currentdb again
*/
var db_current = window.sqlitePlugin.openDatabase(dbCurrent_path , "1.0", "DbCurrent", 10000000);
db_current.transaction(doquerycurrent, on_tran_error, on_tran_success);
}
function doquerycurrent(txc){
// strange behaviour
// this query is executed on storic db if a have opened it
txc.executeSql("SELECT * FROM table1", [],[]);
}
function doquerystoric(txs){
txs.executeSql("SELECT * FROM table1", [],[]);
}
i have tried with attach db form my query but is unsupported from android ????
tx.executeSql("ATTACH DATABASE '/mnt/sdcard/Android/data/myDatabase/DbStoric' as S",[], on_query_success) // this line return error
please help me
thanks

first time running sqlite after app install issue with phonegap/coredava

I am developing an APP using phonegap/coredava while trying to create an access database for the first time after app is installed I am unable to access database but on second run everything working fine how can I fix this my javascript code is below
var dbsize=4*1024;
document.addEventListener("deviceready", onDeviceReady, false);
var dbShell = window.openDatabase("mydb", "1.0", "my db", dbsize);
function onDeviceReady(){
dbShell.transaction(defaultPopulatedb,errorDF,successDF);
}
function defaultPopulatedb(tx){ //creating tables for the first time
tx.executeSql('CREATE TABLE IF NOT EXISTS Userlocation (id INTEGER PRIMARY KEY AUTOINCREMENT, Location TEXT NOT NULL, Locationvalue TEXT NOT NULL)',[],checkfirst,errorTB);
}
function checkfirst(tx)
{
tx.executeSql('SELECT * FROM Userlocation',[],chevals,errorDFS); }
}
function chevals(tx,result)
{
var len =result.rows.length;
if(!len){
tx.executeSql('INSERT INTO Userlocation(Location,Locationvalue) VALUES ("default","default")',[],added,erdf);
}
}
function errorDFS()
{
alert("error");
}
function added()
{
alert("added");
}
function erdf()
{
alert("error adding default");
}
function errorTB()
{
alert("error table");
}
I met this problem once too. You can just simply try{...} catch (ex){...} and ignores the first exception.
Actually, there is a great data access framework for phonegap.

Phonegap deviceready not responding/firing

I am writing a mobile application for storing data locally using Sqlite. For the past 4 days, I have been trying to figure out why database is not creating when Jquery and phonegap are fully loaded. Create statement in sqlite doesn't work and callback functions are not working. The deviceready doesn't work but if checking for sqlite support it fires. The example code is someone's else code but the same thing happened. Can someone please help me?
var jqmReady = $.Deferred(),
pgReady = $.Deferred();
// jqm page is ready
$(document).bind("pageinit", jqmReady.resolve);
// phonegap ready
document.addEventListener("deviceready", pgReady.resolve, false);
// all ready, throw a custom 'onDeviceready' event
$.when(jqmReady, pgReady).then(function(){
$(document).trigger("onDeviceready");
});
function onDeviceReady(){
db.transaction(populateDB, errorCB, successCB);
}
//create table and insert some record
function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerPlayer (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Club TEXT NOT NULL)');
tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Alexandre Pato", "AC Milan")');
tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Van Persie", "Arsenal")');
}
//function will be called when an error occurred
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
//function will be called when process succeed
function successCB() {
alert("success!");
db.transaction(queryDB,errorCB);
}
I haven't run it on an emulator or physical device but from looking at the code I can see one issue right off the back. Try this and see if it helps:
Change
$.when(jqmReady, pgReady).then(function(){
$(document).trigger("onDeviceready");
});
to
$.when(jqmReady, pgReady).then(function(){
onDeviceReady();
});
The reason I suggest that change is because $(document).trigger("onDeviceready") is triggering the 'onDeviceready' event. You don't have a listener setup to catch that event and I assume what you wanted it to do is call the "onDeviceReady()" function.

PhoneGap - How to query SQLite in a for loop?

I have a problem with a SQLite query on PhoneGap application.
I would open a folder and make a select with where clause for every entries in the folder.
I open a for loop, after a select query, but the script uses same variables for all the entries (it remembers only the last record's variables).
The loop reads correctly the number of entries, but it works only with one record's variables, and it works for as many times as there are files in the folder only with these variables.
function readerSuccess(entries) {
var db = window.openDatabase("Test", "1.0", "Demo test", 200000);
db.transaction(function(tx) {
var i;
for (i=0; i<entries.length; i+=1) {
var imgName=entries[i].name;
var upfi=entries[i];
console.log(imgName);
tx.executeSql("SELECT * FROM FOTO WHERE img=?", [imgName], function(tx, result) {
console.log(imgName+" after SQL");
var dataset = result.rows;
console.log("Returned rows = " + dataset.length);
if (dataset.length==0) {
console.log('No rows!');
console.log('File sync: '+imgName);
} else {
console.log('File already sync: '+imgName);
};
}, successCB, errorCB)}}, successCB, errorCB);
};
I have tested with global variables or local variables without a success.
I do not know where to turn!! :(

Categories

Resources