Javascript Cordova Sqlite插件插入具有空值的条目
我在一个Ionic项目中实现了,到目前为止,我已经能够创建一个数据库和表,并通过 我注意到有一个Javascript Cordova Sqlite插件插入具有空值的条目,javascript,sqlite,ionic-framework,ngcordova,Javascript,Sqlite,Ionic Framework,Ngcordova,我在一个Ionic项目中实现了,到目前为止,我已经能够创建一个数据库和表,并通过 我注意到有一个insertCollection函数可用,我试着对它进行测试,我向它传递了一个数组,即使插入了,条目也使用null 这是我的示例表定义: CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text) 我像这样填充了一个数组: for(var i = 0; i < 250000;
insertCollection
函数可用,我试着对它进行测试,我向它传递了一个数组,即使插入了,条目也使用null
这是我的示例表定义:
CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)
我像这样填充了一个数组:
for(var i = 0; i < 250000; i++){
var index = i + 1;
firstname = firstname + ' ' + index;
var entry = {
'firstname' : firstname,
'lastname' : lastname
};
$scope.insertArray.push(entry);
}
$cordovaSQLite.insertCollection($rootScope.db, $scope.insertQuery, $scope.insertArray).then(function(res) {
console.log(res);
}, function (err) {
console.error(err);
});
$scope.select = function() {
$scope.results = [];
var query = "SELECT firstname, lastname FROM people";
$cordovaSQLite.execute($rootScope.db, query).then(function(res) {
if(res.rows.length > 0) {
console.log('select was successful ' + res.rows.length + ' entries');
for(var i = 0; i < $scope.maxItemsToShow; i++){
$scope.results.push(res.rows.item(i));
}
} else {
console.log("No results found");
}
}, function (err) {
console.error(err);
});
}
其中insertQuery
如下所示:
INSERT INTO people (firstname, lastname) VALUES (?,?)
我做了如下选择:
for(var i = 0; i < 250000; i++){
var index = i + 1;
firstname = firstname + ' ' + index;
var entry = {
'firstname' : firstname,
'lastname' : lastname
};
$scope.insertArray.push(entry);
}
$cordovaSQLite.insertCollection($rootScope.db, $scope.insertQuery, $scope.insertArray).then(function(res) {
console.log(res);
}, function (err) {
console.error(err);
});
$scope.select = function() {
$scope.results = [];
var query = "SELECT firstname, lastname FROM people";
$cordovaSQLite.execute($rootScope.db, query).then(function(res) {
if(res.rows.length > 0) {
console.log('select was successful ' + res.rows.length + ' entries');
for(var i = 0; i < $scope.maxItemsToShow; i++){
$scope.results.push(res.rows.item(i));
}
} else {
console.log("No results found");
}
}, function (err) {
console.error(err);
});
}
$scope.select=function(){
$scope.results=[];
var query=“从人员中选择firstname、lastname”;
$cordovaSQLite.execute($rootScope.db,query)。然后(函数(res){
如果(res.rows.length>0){
log('select已成功'+res.rows.length+'entries');
对于(变量i=0;i<$scope.maxItemsToShow;i++){
$scope.results.push(res.rows.item(i));
}
}否则{
console.log(“未找到结果”);
}
},函数(err){
控制台错误(err);
});
}
我试图在一个离子列表上显示结果
,但是firstname和lastname中的值对于所有项目都是空的
导致此问题的原因是什么?函数
insertCollection
需要数组,而不是记录数组。内部数组必须按照问号(作为值的占位符)在sql语句中出现的顺序包含要插入的值
所以你需要写:
for(var i = 0; i < 250000; i++){
var index = i + 1;
firstname = firstname + ' ' + index;
var entry = [firstname, lastname];
$scope.insertArray.push(entry);
}
for(变量i=0;i<250000;i++){
var指数=i+1;
firstname=firstname+“”+索引;
var条目=[firstname,lastname];
$scope.insertArray.push(条目);
}
在填充数组的for循环中,firstname和lastname都没有初始化。firstname和lastname都是从视图中初始化的,我调试过,insertArray确实有我设置的伪数据项,所以这不是初始化问题。这个答案解决了,老实说,我没有找到如何使用该函数的示例,我必须检查代码,甚至意识到这个函数的存在,文档甚至没有提到它。