Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 228595

Ionic SQLite is not working on android device "SELECT Query"

$
0
0

Everything is find in the browser and in the device.
And on the device when I click getData I don't receive any error from my $cordovaSQLite.execute select query
here's my code

app.js
var app = angular.module('starter', ['ionic', 'ngCordova'])

app.run(function($ionicPlatform, $cordovaSQLite, $rootScope) {
$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);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}

if (window.cordova) {
  $rootScope.db = $cordovaSQLite.openDB({ name: "my.db" }); //device
}else{
  $rootScope.db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // browser
}

$cordovaSQLite.execute($rootScope.db, "CREATE TABLE IF NOT EXISTS local_db(id integer primary key, some_text text)")
.then(
    function(res) {
      console.log(res);
      console.log('DB Success');
        // console.log("insertId: " + res.insertId);
        // alert("insertId: " + res.insertId);
    },
    function (err) {
        console.error(err);
    }
  );

});
});

mainCtrl.js
`app.controller('mainCtrl', function($scope, $cordovaSQLite, $rootScope, $ionicPlatform){

$scope.userData = {
	idNum: 1,
	myText: '' };
$ionicPlatform.ready(function() {

$scope.insertData = function(){

    var query = "INSERT INTO local_db (some_text) VALUES (?)";
    $cordovaSQLite.execute($rootScope.db, query, [$scope.userData.myText])
    .then(
		function(res) {
      		console.log("insertId: " + res.insertId);
      		alert("insertId: " + res.insertId);
    	},
    	function (err) {
      		console.error(err);
    	}
	);


};

$scope.getData = function(){

    alert("went here");
    var query = "SELECT some_text FROM local_db WHERE id = ? ";
    $cordovaSQLite.execute($rootScope.db, query, [$scope.userData.idNum])
    .then(
		function(res) {
			console.log(res);
			alert(res.rows[0].some_text);

      		// console.log("insertId: " + res.insertId);
      		// alert(res.rows[0].joke);
    	},
    	function (err) {
      		console.error(err);
      		alert(err);
    	}
	);


};
});//EOF

});`

index.html
`




<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">
-->

<!-- 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>
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/mainCtrl.js"></script>


<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">SQLite</h1>
  </ion-header-bar>
  <ion-content>
    <input type="text" ng-model="userData.myText" style="background-color:gold">
    <button class="button button-block button-positive" ng-click="insertData()">Insert SQL</button>
    <input type="number" ng-model="userData.idNum" style="background-color:gold">
    <button class="button button-block button-positive" ng-click="getData()">Select SQL</button>
  </ion-content>
</ion-pane>



`


Viewing all articles
Browse latest Browse all 228595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>