Having trouble doing insertion in SQLiteDatabase


#1

I am having trouble doing insertion in SQLiteDatabase in ionic framework. I added cordova-sqlite-storage plugin, my problem is data is not being inserted, I am not getting any message when I hit INSERT button, can someone tell my what is my mistake here?

This is my index.html

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

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

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

<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Test app</h1>
</ion-header-bar>
<ion-content ng-controller="MainCtrl">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="Name" ng-model="fullname">
</label>
<label class="item item-input">
<input type="text" placeholder="E-mail" ng-model="email">
</label>
<button class="button button-full button-positive" ng-click="insert(fullname, email)">INSERT</button>
</div><p>{{statusMessage}}</p>
</div>
</ion-content>
</ion-pane>
</body>
</html>

and this is my app.js

var db = null;
var app = angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}

db = $cordovaSQLite.openDB("demo.db");
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS user(id INTEGER PRIMARY KEY AUTOINCREMENT, fullname TEXT, email TEXT)');

});

});

app.controller('MainCtrl', function($scope, $cordovaSQLite) {
$scope.insert = function(fullname, email) {
$cordovaSQLite.execute(db, 'INSERT INTO user (fullname, email) VALUES (?,?)', [fullname, email])
.then(function(result) {
$scope.statusMessage = "data saved";
}, function(error) {
$scope.statusMessage = "error on saving: " + error.message;
});

};
});

#2

See my best example using service pattern :slight_smile:

Regards, Nicholls