jeudi 31 mars 2016

SQLite database Insert query not working for ionic app

I am trying to add cordova SQLite database in my ionic project which is scanning qrcode & inserting details into local database.

My database initialization is this:

var db = null;
var app = angular.module('scanstarter', ['ionic', 'ionic-material', 'ngCordova']);

app.run(function ($ionicPlatform, $ionicLoading, $cordovaGeolocation, $cordovaSQLite) {
$ionicPlatform.ready(function () {

    document.addEventListener("deviceready", function () {
        // Sqlite database initialization
        db = $cordovaSQLite.openDB({name: 'scanstarter.db'});
        // create table for product scan
        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS productScan (id integer primary key, unique_id text, productName text, serialNo text, manufacturer text, department text, time text, latitude text, longitude text, actionRequired text, sync text)");
        // create table for comments
        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS comments (id integer primary key, unique_id text, comment text)");
    }, false);
});

})

My scan function flow is this:

    $scope.goToScan = function () {
    cordova.plugins.barcodeScanner.scan(
            function (result) {
                if (!result.cancelled)
                {
                    if (result.format == "QR_CODE")
                    {
                        var scannedData = result.text;

                        var posOptions = {enableHighAccuracy: true};
                        $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
                            //Get latitude and longitude
                            var latitude = position.coords.latitude;
                            var longitude = position.coords.longitude;
                            //Initial Split with #
                            var splitArray = scannedData.split("#");
                            var UniqueId = splitArray[0];
                            var ProductName = splitArray[1];
                            var SerialNo = splitArray[2];
                            var Manufacturer = splitArray[3];
                            var Department = splitArray[4];
                            //Individual Split with :
                            var UniqueIdArray = UniqueId.split(':');
                            var ProductNameArray = ProductName.split(':');
                            var SerialNoArray = SerialNo.split(':');
                            var ManufacturerArray = Manufacturer.split(':');
                            var DepartmentArray = Department.split(':');

                            if ((UniqueIdArray[0] == 'unique_id') && (ProductNameArray[0] == 'product_name') && (SerialNoArray[0] == 'serial_no') && (ManufacturerArray[0] == 'manufacturer') && (DepartmentArray[0] == 'department')) {
//Insert query part start
                                var query = "INSERT INTO productScan (unique_id, productName, serialNo, manufacturer, department, time, latitude, longitude, actionRequired, sync) VALUES (?,?,?,?,?,?,?,?,?,?)";
                                $cordovaSQLite.execute(db, query, [UniqueIdArray[1], ProductNameArray[1], SerialNoArray[1], ManufacturerArray[1], DepartmentArray[1], Math.floor(Date.now() / 1000), latitude, longitude, 'N', 'N']).then(function (res) {
                                    alert(JSON.stringify(res));

                                }, function (err) {
                                    alert(JSON.stringify(err));
                                });
//Insert query part end
                                $state.go('app.scan');

                            } else {
                                alert('Invalid QR-Code. This QR-Code is not part of Inventory!.');
                            }

                        }, function (err) {
                            $ionicLoading.show({template: 'Kindly check your mobile GPS. GPS must be on!.'});
                            $timeout(function () { // server replies when username or password is incorrect
                                $ionicLoading.hide();
                            }, 3000)
                        });

                    }
                }
            },
            function (error) {
                alert("Scanning failed: " + error);
            }
    );
}

what happens is when i comment the insert query part of cordovaSQLite and build the app and check it then it successfully scan and then navigates to next page. But when i uncomment it and build the apk and run it on my mobile it does not navigate me to next page. I saw the flow working by adding subsequent alerts after each modile of execution and then i found that i am getting stuck at the point where sqlite insert query code starts.

What is the error occuring here i am really not getting. Is there something i am missing?

Thanks in advance for quick response.

Aucun commentaire:

Enregistrer un commentaire