我在将行插入 SQL 数据库时遇到问题。我想将一组对象转换为 javascript 中的 SQL 表。
以下代码仅添加数组的第一个对象。我已经尝试了在 stackoverflow 和其他地方可以找到的所有内容,但无法使其正常工作。
任何帮助,将不胜感激。谢谢。
for (var i = 0; i < arr.length; i++) {
db.save({key:i+"", value:arr[i]}, function(e){
});
}
更新 1:我已将其更改为 mathec 的示例,并稍微缩小了问题范围。
插入的行数取决于插入对象的大小。所以它与处理每个对象所需的时间有关。
我该如何解决这个问题?谢谢。
更新 2:
我在下面采纳了 Robert Young 的建议,并附上了一个独立的例子。
下面的示例仅插入前 5 个元素。如果我删除了测试键中的一些单词文本,所以它只说“单词”一次,那么就会插入 10 个元素。所以现在我确定它与处理每个对象所需的时间有关。
<html>
<head>
<script src="jquery.js"></script>
<script src="lawnchair.js"></script>
<script type='text/javascript'>
var db = "";
var arr = [];
arr.push({name:"a1", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a2", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a3", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a4", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a5", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a6", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a7", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a8", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a9", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a10", test:"word word word word word word word word word word word word word word "});
arr.push({name:"a11", test:"word word word word word word word word word word word word word word "});
$(function() {
db = new Lawnchair({table:'t50'}, function(e){
for ( i = 0; i < arr.length; i++) {
(function(i) {
add_row(i);
}(i));
}
});
});
function add_row(i) {
db.save({key:i+"", value:arr[i]}, function(e){
});
}
</script>
</head>
<body>
</body>
</html>
更新 3:我使用了罗伯特建议的代码,并提出了以下与三个小元素一起使用的代码。所以我改变了第一个元素,使它比其他元素更大来测试它。第一个元素没有添加,最后两个是。处理数组有时间限制吗?
<html>
<head>
<script src="jquery.js"></script>
<script src="lawnchair.js"></script>
<script type='text/javascript'>
var arr = [];
var db = "";
$(function() {
db = new Lawnchair({table:'t51'}, function(e){
arr=[{key:"k1", value:"v1. Because the contents of this element are larger than the others it will not be added for some reason. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "}
,{key:"k2", value:"v2"}
,{key:"k3", value:"v3"}]
db.batch(arr, function () {
db.all(function (recs) { for (r in recs) {console.log(recs[r].key +"| "+ recs[r].value) } });
});
});
});
</script>
</head>
<body>
</body>
</html>