Reputation: 810
I am having trouble doing CRUD operations in SQLiteDatabase in ionic framework. This is my main "index.html" page, where I am displaying my div by replacing another div using "replaceWith()", which I learned recently!
<meta charset="utf-8">
<meta name="viewport"
content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Still Learning</title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/jquery-1.11.3.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<script>
$(document).ready(function(){
$("#insertbtn").click(function(){
$(".toshow").replaceWith('<div class="toshow" ng-controller="MyController">\
<div class="list">\
<label class="item item-input">\
<input type="text" placeholder="Full Name" ng-model="fullname">\
</label>\
<label class="item item-input">\
<input type="text" placeholder="Address" ng-model="address">\
</label>\
<label class="item item-input">\
<input type="text" placeholder="Email" ng-model="email">\
</label>\
<label class="item item-input">\
<input type="number" placeholder="Age" ng-model="age">\
</label>\
<label class="item item-input">\
<input type="password" placeholder="Password" ng-model="password">\
</label>\
<label class="item item-input">\
<textarea placeholder="Details" ng-model="details"></textarea>\
</label>\
<br /><button class="button button-outline button-positive" ng-click="insert(fullname, address, email, age, password, details)">INSERT</button>\
</div><p>{{ statusMessage }}</p>\
</div>');
});
});
</script>
Now here, in this replaced div, I add some values and want to insert that into the SQLite Database!
And this is my app.js file:
var db=null;
angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform) {
$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("sample.db");
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS User(id INTEGER PRIMARY KEY AUTOINCREMENT, fullname TEXT, address TEXT,email TEXT, age INTEGER, password TEXT, details TEXT)');
});
})
.controller('MyController', function($scope, $cordovaSQLite) {
$scope.insert = function(fullname, address, email, age, password, details) {
$cordovaSQLite.execute(db, 'INSERT INTO User (fullname, address, email, age, password, details) VALUES (?,?,?,?,?,?)', [fullname, address, email, age, password, details])
.then(function(result) {
$scope.statusMessage = "Data saved successful, cheers!";
}, function(error) {
$scope.statusMessage = "Error on saving: " + error.message;
})
}
})
There is no error watsoever, but my data is not being inserted!! Can someone tell me whats my mistake here? THankx in advance.....
Upvotes: 1
Views: 5556
Reputation: 218
Latest version of cordovaSQLite plugin request location property as a mandatory property. so you need to put; db=$cordovaSQLite.openDB({name: "sample.db",location: 1}); instead of db = $cordovaSQLite.openDB("sample.db"); in your app.js file. for more details follow this. https://sumithmadhushan.wordpress.com/2016/04/12/use-sqlite-in-ionic-framework/
Upvotes: 1
Reputation: 549
.run(function($ionicPlatform , **$cordovaSQLite**)
{
//all the code remain same here
}
Here you need to add one extra parameter in .run()
function that is $cordovaSQLite
This will help you :)
Upvotes: 4
Reputation: 10857
Could be a race condition. Your first query could execute after the second, and your second one could execute before the first one ends. You need to use the success handler of the transaction with the query to then execute your insert. I'd probably switch to using a Service for your persistence - that way you can hide some of these details away from the controller.
Upvotes: 0
Reputation: 541
You have to install the Cordova sqlite plugin from https://github.com/litehelpers/Cordova-sqlite-storage otherwise it will not work.
Upvotes: -1