This is a migrated thread and some comments may be shown as answers.
Sqlite doesnt run on device
7 Answers 153 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Keen
Top achievements
Rank 1
Keen asked on 15 Jul 2013, 08:35 AM

//Based on http://www.html5rocks.com/en/tutorials/webdatabase/todo/

document.addEventListener("deviceready", init, false);

 

 

var app = {};

app.db = null;

 

app.openDb = function() {

if(window.sqlitePlugin !== undefined) {

app.db = window.sqlitePlugin.openDatabase("CALCMENU");

alert("SQLITE");

} else {

// For debugin in simulator fallback to native SQL Lite

console.log("Use built in SQL Lite");

app.db = window.openDatabase("CALCMENU", "1.0", "Cordova Demo", 500000);

}

}

 

app.createTable = function() {

var db = app.db;

db.transaction(function(tx) {

//code for creating

});

 

}

 

//some functions

function init() {

navigator.splashscreen.hide();

app.openDb();

app.createTable();

uuid = device.uuid;

App = new downloadApp();

folderName = "RecipeImages";

App.run(folderName);

$.support.touchOverflow = true;

$.mobile.touchOverflowEnabled = true;

cordova.require('cordova/plugin/powermanagement').acquire(

function() { alert( 'successfully acquired full wake lock' ); })

 

}




The above code is my whole code in my main.js . When using the sample SQLite apps of Icenium on device, its running that he used SQLite as database. But I applied the codes that I need to the my apps,but when I deploy on device, He used the websql not the SQLite cordova. Am I missing something on my codes?

thanks
Keen

7 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 15 Jul 2013, 08:42 AM
Hello Keen,

Be sure to include the SQLite plug-in into your app, see Enable and Configure Integrated Plugins help article. As you already have a runnable demo at your disposal, you can compare the differences or use that demo as a starting point for your app i.e. add the files for your app into the sqlite demo and continue your development there.

Regards,
Steve
Telerik

Looking for tips & tricks directly from the Icenium team? Check out our blog!
Missed our first webinar, watch it here.
Share feedback and vote for features on our Feedback Portal.
0
Martin Roussel
Top achievements
Rank 1
answered on 07 Jan 2014, 02:52 PM
Hi, im also experiencing problems with SQLite on three different devices (Android Phone 2.3.4, Android tablet 4.0.3 and iPod 6.1.5). First on the three devices, the SQLite sample runs fine. The problem is when I want to integrate it in my current app. On all simulators, my app is fine also (using built-in SQLite). Ive enabled the plugin in my project properties and running Cordova 3.2.0.

On the two Android devices it fails at this line:
app2.db = window.sqlitePlugin.openDatabase("Todo");

On the iOS, everything seems to work well except I sometimes get "disk I/O error" when executing this (It seems to always occur when refreshing the app without closing it):

tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (?,?)",
                             ['hello', new Date()],
                             app2.onSuccess,
                             app2.onError);

here is my app.js (which shares some logic for Skin...please dont min the bunch of alert im using to test)
(function (global) {
    var mobileSkin = "",
        app = global.app = global.app || {};
    var app2 = {};
    app2.db = null;
     
    document.addEventListener("deviceready", function () {
        app.application = new kendo.mobile.Application(document.body, { layout: "tabstrip-layout" });
        navigator.splashscreen.hide();
        app2.openDb();
        app2.createTable();
        app2.deleteTodo();
        app2.addTodo('allo');
        
    }, false);
     
    document.addEventListener("touchstart", function() {}, false);
 
    app2.openDb = function() {
       alert("opening");
      if (device.uuid == "e0101010d38bde8e6740011221af335301010333" || device.uuid == "e0908060g38bde8e6740011221af335301010333") {
        // For debugin in simulator fallback to native SQL Lite
        alert("Use built in SQL Lite");
        app2.db = window.openDatabase("Todo", "1.0", "Cordova Demo", 200000);
    }
    else {
         alert("sqlitePlugin");
        app2.db = window.sqlitePlugin.openDatabase("Todo");
        alert("OK");
    }
       alert(app2.db);
    };
     
    app2.createTable = function() {
        var db = app2.db;
        alert("create");
        db.transaction(function(tx) {
             alert("create in");
            tx.executeSql("CREATE TABLE IF NOT EXISTS todo(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)", []);
        });
        alert("create out");
    };
     
    app2.onError = function(tx, e) {
        alert("Error: " + e.message);
    };
           
    app2.onSuccess = function(tx, r) {
        alert("success");
         
        var render = function (tx, rs) {
             
            for (var i = 0; i < rs.rows.length; i++) {
               alert(i + " " + rs.rows.item(i));
            }
           
             
        }
     
        var db = app2.db;
        db.transaction(function(tx) {
            tx.executeSql("SELECT * FROM todo", [],
                          render,
                          app2.onError);
        });
    };
   
    app2.addTodo = function(todoText) {
            var db = app2.db;
            alert("insert");
            db.transaction(function(tx) {
                alert("insert in");
                var addedOn = new Date();
                tx.executeSql("INSERT INTO todo(todo, added_on) VALUES (?,?)",
                              [todoText, addedOn],
                              app2.onSuccess,
                              app2.onError);
            });
        alert("insert out");
        };
     
    app2.deleteTodo = function(id) {
        var db = app2.db;
        db.transaction(function(tx) {
            tx.executeSql("DELETE FROM todo",[]);
        });
    }
     
 
    app.changeSkin = function (e) {
        if (e.sender.element.text() === "Flat") {
            e.sender.element.text("Native");
            mobileSkin = "flat";
        }
        else {
            e.sender.element.text("Flat");
            mobileSkin = "";
        }
 
        app.application.skin(mobileSkin);
    };
     
    
         
})(window);

Is there something im doing wrong?

TIA
0
Steve
Telerik team
answered on 10 Jan 2014, 07:39 AM
Hello Martin,

I've used your t***-*****n app to test locally on Android, but did not notice any errors or problems compared to what I observed in the Icenium simulator in the test tab. Can you share exact steps I need to follow in your app that fail?

Regards,
Steve
Telerik
You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
Looking for tips & tricks directly from the Icenium team? Check out our blog!
Share feedback and vote for features on our Feedback Portal.
0
Martin Roussel
Top achievements
Rank 1
answered on 10 Jan 2014, 12:17 PM
Thanks for the reply Steve.

On both of my Android devices, I only have to open the app and two alert should pop at startup. 1-"sqlitePlugin" and 2-"OK". On both devices, I never see the "OK" one, but the same code works for the sample-sqlite (I see the "OK"). Here is the logic:

if (device.uuid == "e0101010d38bde8e6740011221af335301010333" || device.uuid == "e0908060g38bde8e6740011221af335301010333") {
        // For debugin in simulator fallback to native SQL Lite
        app.db = window.openDatabase("Todo", "1.0", "Cordova Demo", 200000);
    }
    else {
        alert("sqlitePlugin");
        app.db = window.sqlitePlugin.openDatabase("Todo");
        alert("OK");
    }
0
Martin Roussel
Top achievements
Rank 1
answered on 13 Jan 2014, 03:04 PM
UPDATE: ive succeed to make it work on the android tablet by moving the app.openDB() and app.createTable() from the deviceready function (app.js) to the $( document ).ready() function on the page itself. This also works on the iOS but not on the android phone.

UPDATE2 : ive succeed to make it work (android tablet and iOS only, not android phone) by putting the openDB and createTable back in the app.js like it was... but I need to have this div on the page to make it work:

<div data-role="view"></div>

no clue why
0
Steve
Telerik team
answered on 13 Jan 2014, 03:46 PM
Hi Martin,

Glad you found a working solution. How did you attach deviceready event in your app in a first place? As per Cordova docs, it should be attached in onload event.

Regards,
Steve
Telerik
You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
Looking for tips & tricks directly from the Icenium team? Check out our blog!
Share feedback and vote for features on our Feedback Portal.
0
Martin Roussel
Top achievements
Rank 1
answered on 13 Jan 2014, 03:58 PM
Steve,

like this 
document.addEventListener("deviceready", function () {
        app.application = new kendo.mobile.Application(document.body, { layout: "tabstrip-layout" });
        navigator.splashscreen.hide();
        app.openDb();
        app.createTable(); 
    }, false);

it is not a real solution since it should work on my android phone (like the sample)
Tags
General Discussion
Asked by
Keen
Top achievements
Rank 1
Answers by
Steve
Telerik team
Martin Roussel
Top achievements
Rank 1
Share this question
or