Angularjs can';t在ionic 1中的sqlite表中插入数据
我对爱奥尼亚一号还不熟悉。我将我的应用程序与sqlite连接。我想查看插入的表数据。并在控制台中显示插入的数据。但是,我不明白为什么行长度仍然是0。控制台上没有错误。以下是我的控制器代码片段:Angularjs can';t在ionic 1中的sqlite表中插入数据,angularjs,sqlite,ionic-framework,Angularjs,Sqlite,Ionic Framework,我对爱奥尼亚一号还不熟悉。我将我的应用程序与sqlite连接。我想查看插入的表数据。并在控制台中显示插入的数据。但是,我不明白为什么行长度仍然是0。控制台上没有错误。以下是我的控制器代码片段: .controller('salesCtrl', ['$scope', '$stateParams','$cordovaSQLite', // The following is the constructor function for this page's controller. See https:
.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()
函数
<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>
点击
600
需要检查的一件事是确保您在deviceready启动时或之后打开DB
如果在deviceready之前执行此操作,window.openDatabase将成功,但$cordovaSQLite.openDB可能会失败。每次您想要运行sqlite时,都使用ionicPlatform ready包装
.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);
});
}
});
}
}])
select语句返回result.rows,对于insert语句,返回插入的对象,因此返回console.log(“insertId:+res.insertId”)@digit如果我想查看插入的值,该怎么办。您可以逐个console.log整个对象。log('object:',result)是的,我知道我可以,但是,即使我插入了数据。我在对象中看不到任何数据。甚至连我桌子的名字都没有@digitand我看到行长度为0,但插入了更多。您的代码显示错误:TypeError:无法读取nullTry的属性'transaction',请将位置参数添加到$cordovaSQLite.openDB({name:“pos.db”,location:“default”});