参数化的select查询在使用sqlite数据库的angular js中不起作用

Parameterized select query not working in angular js using sqlite database

本文关键字:angular 数据库 js 不起作用 sqlite select 查询 参数      更新时间:2023-09-26

我想使用where子句获取数据,但无法执行。
(例如:如果我在username中输入"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);
    });
  }
});

只需使用以下代码在sqlite数据库的angular 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);
        });
      }