参数化选择查询使用sqlite数据库在角度js中不起作用

时间:2016-01-07 05:30:37

标签: javascript android angularjs sqlite ionic-framework

我想使用where子句获取数据,但我无法这样做 (例如:如果我在用户名中输入" sid"它应该提供与" sid")相关联的所有记录。请建议我需要改变什么。
以下是我的代码:

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);
    });
  }

});

1 个答案:

答案 0 :(得分:0)

在sqlite数据库的角度js中使用以下代码进行参数化选择查询

$scope.login = function(firstname,lastname) {
     $cordovaSQLite.execute(db,"SELECT firstname, lastname FROM people where firstname=? and lastname=?",[firstname,lastname]).then(function(res) {
          if(res.rows.length >0)
            window.location="login.html";
        }, function (err) {
          console.error(err);
        });
      }