无法在ionic 1的sqlite表中插入数据 [英] can't insert data in sqlite table in ionic 1
问题描述
我对Ionic 1还是陌生的.我已将我的应用程序与sqlite连接.我想查看插入的表数据.并在控制台中显示插入的数据.但是,我不知道为什么行长度仍为0.并且在控制台上没有错误.这是我的控制器代码段:
I am new to ionic 1. I connected my app with sqlite. I want to see the table data that are inserted. And display the inserted data in the console. But, I can't figure out Why row length is still 0. And there is no error on console. Here's my controller code snippet:
.controller('salesCtrl', ['$scope', '$stateParams','$cordovaSQLite', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,$cordovaSQLite) {
var db=null;
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "pos.db" }); //device
console.log("not in browser");
}else{
db = window.openDatabase("pos.db", '1', 'my', 1024 * 1024 * 100); // browser
console.log("browser");
}
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS items(id integer primary key AUTOINCREMENT,firstname text NOT NULL,lastname text NOT NULL)");
$scope.insert=function(){
var query="INSERT into items(firstname,lastname) VALUES(?,?)";
$cordovaSQLite.execute(db,query,["name1","name2"]).then(function(result){
console.log(result.rows);
},function(error){
console.log(error);
})
}
}])
这是我的模板页面代码段,当有人单击按钮时,它将调用 insert()
函数.
and here is my template page code snippet in which when someone click the button it will call the insert()
function.
<ion-item class="item-icon-left" id="sales-list-item4" ui-sref="menu.itemQuantity">
<i class="icon ion-android-checkbox-blank"></i>
<button class="button button-icon icon ion-ios-cart-outline" ng-click="insert()"> click</button>
<span class="item-note">600 </span>
</ion-item>
推荐答案
要检查的一件事是确保在deviceready启动后或之后打开数据库.
One thing to check is be sure that you are opening the DB in or after deviceready has fired.
如果在准备好设备就绪之前执行该操作,则window.openDatabase将成功,但是$ cordovaSQLite.openDB可能会失败.每当您要运行sqlite时,都可以使用ionicPlatform进行包装.
If you are doing it before deviceready the window.openDatabase will succeed but the $cordovaSQLite.openDB will likely fail. Wrapped with ionicPlatform ready everytimes you want to run the sqlite.
.controller('salesCtrl', ['$scope', '$stateParams','$cordovaSQLite', '$ionicPlatform',
function ($scope, $stateParams,$cordovaSQLite, $ionicPlatform) {
$ionicPlatform.ready(function () {
var db=null;
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "pos.db" }); //device
console.log("not in browser");
} else{
db = window.openDatabase("pos.db", '1', 'my', 1024 * 1024 * 100); // browser
console.log("browser");
}
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS items(id integer primary key AUTOINCREMENT,firstname text NOT NULL,lastname text NOT NULL)");
});
$scope.insert=function(){
$ionicPlatform.ready(function () {
var query="INSERT into items(firstname,lastname) VALUES(?,?)";
$cordovaSQLite.execute(db,query,["name1","name2"]).then(function(result){
console.log(result.rows);
},function(error){
console.log(error);
});
}
});
}
}])
这篇关于无法在ionic 1的sqlite表中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!