storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 715 forks source link

$cordovaSQLite.execute skipped, not working #583

Open galders opened 7 years ago

galders commented 7 years ago

Hi everybody,

I have tried lots of things without success and I would much appreciate if anyone could help me, because it seems that $cordovaSQLite.execute is not being executed nor throwing any exception.

In app.js in the .run part:

var db;

angular.module('starter', ['ionic', 'starter.controllers', 'ngCordova', 'ngAnimate'])

        .run(function ($ionicPlatform, $cordovaSQLite) { //, $cordovaSQLite
            $ionicPlatform.ready(function () {
                // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
                // for form inputs)
                if (window.cordova && window.cordova.plugins.Keyboard) {
                    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                    cordova.plugins.Keyboard.disableScroll(true);
                }

                if (window.StatusBar) {
                    // org.apache.cordova.statusbar required
                    StatusBar.styleDefault();
                }

                if (window.cordova) {
                    //Creación de la base de datos si es que no existe
                    //var db = $cordovaSQLite.openDB({name: "mydb.db"});
                    db = $cordovaSQLite.openDB({name: "mydb.db", iosDatabaseLocation: 'default'});
                } else {
                    db = window.openDatabase("mydb.db", '1', 'mydb', 1024 * 1024 * 100); // browser
                }
            });
        })

Then in controller.js

controller('DownloadDB', function ($scope, $ionicPlatform, $cordovaSQLite ,$http) {
            $ionicPlatform.ready(function () {                              
                alert(db); //Is an objet
                alert(JSON.stringify(db)); //Shows an complete object, looks right openargs: default: "dblocation"....
                try {
                    for (var i = 0; i < queries.length; i++) {
                        //alert('aki1');
                        query = queries[i];
                        alert(query); //Shows a real query

                        $cordovaSQLite.execute(db, query).then(
                            function (result) {
                                alert('query ' + query); //never executed
                            },
                            function (error){
                                alert(error); //never executed
                            }
                        );
                    }
                } catch (e) {
                    alert("693"); //never executed
                    alert(e);       //never executed
                }

In the browser everything runs perfect but when I try to execute in my Android Phone no way.

Thank you very much in advance,

galds

brodycj commented 7 years ago

I think you need to use the $ionicPlatform.ready callback. You may want to take a look at this link: http://nextflow.in.th/en/2015/easy-way-to-work-with-sqlite-database-in-ionic-framework/

UPDATE: To be documented as a pitfall

galders commented 7 years ago

Hi,

Thank you very much for your response.

Before asking here I saw that article that is great and maybe I'm not understanding you, but I was using $ionicPlatform.ready callback. So, I gave the code to a friend and the project was working in Android properly and I tried Ionic Cloud with good result so I think is something related to my installation in Windows 10. I will check it properly in the future, during the meantime I will compile using Ionic Cloud.

Thanks again!