【问题标题】:Cordova sqlite plugin - Cannot read property ' item'Cordova s​​qlite 插件 - 无法读取属性“项目”
【发布时间】:2016-12-31 13:38:04
【问题描述】:

我想通过 ionic 进行一个简单的查询,我使用了 sqlite 插件,我尝试了几个语句(插入、更新、删除)都可以正常工作。

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

Cannot read property 'item' of undefinedCannot read property 'item' of undefined

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

这是完整的代码

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

                });
          
                
                

            })

        });




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

【问题讨论】:

    标签: sqlite cordova ionic-framework


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 不客气! executeSql 的第一个参数是一个对象,您可以在其上执行一些 sqlite 语句,就像您之前已经在一个步骤中所做的那样。第二个参数保存语句的实际结果。当从“tx”对象调用 executeSql 时这是正确的,但是当从“db”对象调用它时,第一个参数已经是查询的结果,如您所见 here。希望能帮助到你! PS:请把我的答案标记为正确;)
    • PS: db-plugins 有很多,executeSql 是否在每个插件中的工作方式都一样不能保证。
    • 上面的 db-plugin 和你使用的内置 sqlite-adapter 非常相似。
    • 谢谢我明白了,你能解释一下我使用的 sqlite pulign 和 ngCordova s​​qlite 插件有什么区别吗?
    • 这两个插件的主要区别在于您可以传递的选项。尽管您可以在应用程序中存储多少数据存在物理限制,但使用内置插件开始时您并不局限于某个存储量(因为您必须传递一个数字作为最后一个参数),而您不能使用此plugin 定义特定的存储量。此外,此插件会打印出它所做的所有交易,因此在您调试时更容易获得您的应用因错误而停止的线索
    猜你喜欢
    • 2016-01-09
    • 2018-05-03
    • 1970-01-01
    • 2020-05-12
    • 2017-06-30
    • 2016-03-27
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    相关资源
    最近更新 更多