Cordova s​​qlite插件 - 无法读取属性'项目'

时间:2016-12-31 13:38:05

标签: sqlite cordova ionic-framework

我想通过离子我使用sqlite插件做一个简单的查询,我尝试了几个语句(插入,更新,删除)一切正常。

但是我遇到了select语句的问题

  

无法阅读属性' item' of undefinedCannot read property' item'未定义的

注意:我之前创建了一个名为cars的表,其中包含两列类型和年份

这里是完整的代码



// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var app = angular.module('starter', ['ionic']);

app.run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {
        if (cordova.platformId === "ios" && window.cordova && window.cordova.plugins.Keyboard) {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

            // Don't remove this line unless you know what you are doing. It stops the viewport
            // from snapping when text inputs are focused. Ionic handles this internally for
            // a much nicer keyboard experience.
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }


        // Select from table 


        var db = null;

        document.addEventListener('deviceready', function () {
            //db = window.sqlitePlugin.openDatabase({ name: 'demo.db', location: 'default' });
            db = window.openDatabase("localDB", "1.0", "Cordova Demo", 200000);

            db.transaction(function (tx) {
                var query = 'SELECT *  FROM cars';
                tx.executeSql(query, [], function (res)
                {
                    alert(res.rows.item(0).type);

                });
          
                
                

            })

        });




    });
});
&#13;
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!--For users deploying their apps to Windows 8.1 or Android Gingerbread, platformOverrided.js
    will inject platform-specific code from the /merges folder -->
    <script src="js/platformOverrides.js"></script>

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app3.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">SQLITE DATABASE </h1>
      </ion-header-bar>
      <ion-content>
  

      </ion-content>
    </ion-pane>
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

试试这个:

tx.executeSql(query, [], function (tx,res){
       alert(res.rows.item(0).type);
});