离子列表中的SQLite数据输入

时间:2016-06-02 12:37:21

标签: javascript angularjs sqlite ionic-framework

我在离子列表中输入数据有些问题。我在StackOverflow中读了一些,但我的问题并没有解决。我想在屏幕上的项目中输入DB数据。我是AngularJS和Ionic的新手,请帮忙!如果你能解决我的问题,我将非常感激!
附:对不起我的英语不好。
litedb.js



var db = null;

angular.module('starter.litedb', ['ngCordova'])

// db controller
.controller("LitedbCtrl", function($scope,$cordovaSQLite,$ionicPopup){
    $scope.insert = function(cardnum, datat, eventt, sum, category, total) {
      var query = "INSERT INTO smsdata (cardnum, datat, eventt, sum, category, total) VALUES (?, ?, ?, ?, ?, ?)";
      $cordovaSQLite.execute(db,query, [cardnum, datat, eventt, sum, category, total]).then(function(result){
        console.log("INSERT ID ->" + result.insertId);
      }, function(error){
        console.log(error);
      })
    }
        $scope.selectAll = function(){
        var	alldataSMS = [];
      var query = "SELECT * FROM smsdata";
      $cordovaSQLite.execute(db,query,[]).then(function(result){
          if(result.rows.length>0){
            console.log("SELECTED ->" + result.rows.item(0).id + " " +
            	result.rows.item(0).category);
            	for (var i = 0; i < result.rows.length; i++) {
// cardnum, datat,  eventt, sum, category, total		
            		alldataSMS.push({
            			id: result.rows.item(i).id,
            			cardnum: result.rows.item(i).cardnum,
            			datat: result.rows.item(i).datat,
            			eventt: result.rows.item(i).eventt,
            			sum: result.rows.item(i).sum,
            			category: result.rows.item(i).category,
            			total: result.rows.item(i).total
            		})
            	}
            	 $scope.$apply();
          } else {
            console.log("NO ROWS EXIST");
          }
      
      }, function(error){
          console.log(error);
      })
    }

})
&#13;
<ion-view view-title="History">
<ion-nav-buttons side="right"> 
     <button class="button button-icon ion-plus"> </button>
</ion-nav-buttons> 
<ion-pane ng-controller="LitedbCtrl">
  <ion-content >
    <button class="button" ng-click="selectAll()">SelectALL</button>
    <ion-list>
    	<ion-item ng-repeat="fsdata in alldataSMS">
    	<h3>{{fsdata.eventt}}</h3>
    	{{fsdata.datat}}
    	{{fsdata.sum}}
    	{{fsdata.category}}
    	{{fsdata.total}}
    	</ion-item> 
    </ion-list>
   
  </ion-content>
  </ion-pane>
</ion-view>
&#13;
&#13;
&#13;


app.js

angular.module('starter', ['ionic', 'starter.controllers','starter.chart',
  'starter.litedb'])

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

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
    // localDB.sync(remoteDB, {live: true});
    db = $cordovaSQLite.openDB({name: "app.db", location: 'default'});
   $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS smsdata (id integer primary key, cardnum text, datat text, eventt text, sum text, category text, total text)");
  });
})


和控制台日志

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我的文件中使用了另一种方法razer。阅读Github plugin repo 。和我的 litedb.js

&#13;
&#13;
var db = null;

angular.module('starter.litedb', ['ionic','ngCordova'])

// db controller
.controller("LitedbCtrl", function($scope, $ionicPlatform, $cordovaSQLite){

    var db = window.sqlitePlugin.openDatabase({ name: 'my.db', location: 'default' }, function () {
    db.executeSql("CREATE TABLE IF NOT EXISTS smsdata (id integer primary key, cardnum text, datat text, eventt text, sum text, category text, total text)");
}, function (error) {
    console.log('Open database ERROR: ' + JSON.stringify(error));
});

function addItem(cardnum, datat, eventt, sum, category, total) {

    db.transaction(function (tx) {

        var query = "INSERT INTO smsdata (cardnum, datat, eventt, sum, category, total) VALUES (?, ?, ?, ?, ?, ?)";

        tx.executeSql(query, [cardnum, datat, eventt, sum, category, total], function(tx, res) {
            console.log("insertId: " + res.insertId);
            console.log("rowsAffected: " + res.rowsAffected);
        },
        function(tx, error) {
            console.log('INSERT error: ' + error.message);
        });
    }, function(error) {
        console.log('transaction error: ' + error.message);
    }, function() {
        console.log('transaction ok');
    });
}
// addItem('1452', '13.02.2016', 'покупка', '400' ,'Продукты', '800');
// addItem('1452', '15.02.2016', 'зачесление', '1100' ,'Банкомат', '1900');
// addItem('1452', '19.02.2016', 'покупка', '75' ,'Продукты', '1825');
// addItem('1452', '20.02.2016', 'покупка', '290' ,'Здоровье', '1535');
// addItem('1452', '25.02.2016', 'покупка', '950' ,'Развлечения', '585');
// addItem('1452', '29.02.2016', 'покупка', '49' ,'Продукты', '536');

function selAll(){

	$scope.alldataSMS = [];

	db.executeSql("SELECT * FROM smsdata", [], function (result) {
		 for(var x = 0; x < result.rows.length; x++) {
  // console.log('cardnum: ' + result.rows.item(x).cardnum +
  //  ' Data: ' + result.rows.item(x).datat +
  //   ' Event: ' + result.rows.item(x).eventt +
  //   ' Sum: ' + result.rows.item(x).sum + 
  //   ' category: ' + result.rows.item(x).category +
  //   ' total: ' + result.rows.item(x).total);

  	$scope.alldataSMS.push({id:result.rows.item(x).id,
            			cardnum:result.rows.item(x).cardnum,
            			datat:result.rows.item(x).datat,
            			eventt:result.rows.item(x).eventt,
            			sum:result.rows.item(x).sum,
            			category:result.rows.item(x).category,
            			total:result.rows.item(x).total});
		};
}, function(error) {
  console.log('SELECT error: ' + error.message);
});
}

selAll();

function closeDB() {
    db.close(function () {
        console.log("DB closed!");
    }, function (error) {
        console.log("Error closing DB:" + error.message);
    });
}

});
&#13;
&#13;
&#13;