mercredi 6 janvier 2016

Parameterized select query not working in angular js using sqlite database

I want to fetch data using where clause but I am unable to do so.
(For eg: If I enter "sid" in username it should give all the records linked with "sid"). Please suggest me what needs to be changed .
Below is my code:

var example = angular.module('starter', ['ionic', 'ngCordova'])
  .run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
      if(window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      }
      if(window.StatusBar) {
        StatusBar.styleDefault();
      }
      db = window.openDatabase("my.db","1.0","my.db",100000);
      $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
      window.alert("Database Created .. !")
    });
  });  
example.controller("ExampleController", function($scope, $cordovaSQLite) {

  $scope.insert = function(firstname, lastname) {

    var query = "INSERT INTO people (firstname,lastname) VALUES (?,?)";
    $cordovaSQLite.execute(db, query, [firstname, lastname]).then(function(res) {
      console.log("INSERT ID -> " + res.insertId);
    }, function (err) {
      console.error(err);
    });
  }

  $scope.select = function(firstname) {

    $cordovaSQLite.execute(db,"SELECT firstname, lastname FROM people where lastname=?",[firstname]).then(function(res) {
      if(res.rows.length >0)
       for(var i=0;i<=res.rows.length;i++){
        window.alert("SELECTED -> " + res.rows.item(i).firstname + " " + res.rows.item(i).lastname);
      } else {
        console.log("No results found");
      }
    }, function (err) {
      console.error(err);
    });
  }

});

Aucun commentaire:

Enregistrer un commentaire