Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix prepare statement failed with error: "1" - using SQLite?

I created a factory with a function that deliver a query for $cordovaSQLite.execute. I am sure that the query is correctly because I run that right in the database using SQLiteBrowser in this works perfectly. In the console I have a message informing that the database was opened. But this function is not returning the data that I need.

I received this error: prepare statement failed with error: 1.

This is my code:

factotyDatabase.js
var app = angular.module('anotei');

app.factory('factoryDatabase', function($cordovaSQLite, $ionicLoading, $q){

var currentDB = undefined;
var DATABASE_NAME = 'anotei.db';
var PATH = 'anotei.db';

return{
    init: function(){
        if(currentDB == undefined){
            currentDB = window.sqlitePlugin.openDatabase({
                name: DATABASE_NAME,
                createFromResources: PATH
            });
        }
        return currentDB;
    },

    executeQuery: function(deliveredQuery, fields){
        var defer = $q.defer();
        $ionicLoading.show();

        var resp = $cordovaSQLite.execute(currentDB, deliveredQuery, fields);
        resp.then(
            function(execution){
                defer.resolve(execution);
                $ionicLoading.hide();
            },
            function(error){
                $ionicLoading.hide();
                    defer.reject(error);
                    console.log(error);
                }
            );
            return defer.promise;
        }


    }
});

// This is the service with the function that calls the factory

var app = angular.module('anotei');

app.service('serviceSubject', function($q, factoryDatabase){

    return{
        getSubjects: function(){
        var defer = $q.defer();

        var resp = factoryDatabase.executeQuery('select * from materias');

        resp.then(
            function(resultSet){
                var listMateria = [];

                for(var i = 0; i < resultSet.rows.length; i++){
                    var materia = {};

                    materia.id_materia = resultSet.rows.item(i).id_materia;
                    materia.nome = resultSet.rows.item(i).nome;
                    materia.max_faltas = resultSet.rows.item(i).max_faltas;
                    materia.professor = resultSet.rows.item(i).professor;
                    materia.email_prof = resultSet.rows.item(i).email_prof;
                    materia.num_faltas = resultSet.rows.item(i).num_faltas;

                    listMateria.push(materia);
                }
                defer.resolve(listMateria);
            },
            function(error){
                console.log(error);
                }
            );

            return defer.promise;
        }
    }

});
like image 337
Everton Castro Avatar asked Nov 10 '22 08:11

Everton Castro


1 Answers

I solved the problem.

I was just using an old version of $cordovaSQLite. I just uninstalled that and run again:

Plugin add for cordova https://github.com/litehelpers/Cordova-sqlite-storage.git

Problem was fixed. Thanks!!!

like image 109
Everton Castro Avatar answered Nov 14 '22 22:11

Everton Castro