问题与循环SQLite中选择查询?

编程入门 行业动态 更新时间:2024-10-28 19:26:57
本文介绍了问题与循环SQLite中选择查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图遍历一个动态数组作为选择查询的输入。选择查询器不点火所有的数组元素。我在for循环,问题是循环发射第一和选择查询数组的最后一个元素只能进行写SELECT查询。可能这是由于异步函数。我怎样才能解决这个问题,我的code是如下,

I am trying to iterate an dynamic array as input for select query. The select query is not firing for all array elements. I wrote select query in for loop, the problem is loop is firing first and the select query is performing only for last element of array. may be this due to asynchronous function. How can I resolve this, My code is as follows,

function sendCategoryDetailsNew(myLocation) { var myLocationcoordinates = new Array(); var db = window.sqlitePlugin.openDatabase({name: "MYDB"}); for (var i = 0; i < myLocation.length; i++) { var locationName = myLocation[i]; alert(locationName); db.transaction(function (tx) { tx.executeSql("select Coordinates from Locationlog WHERE Location = '"+locationName+"';", [], function (tx, res) { for (var i = 0; i < res.rows.length; i++) { alert("Location : "+locationName+ " Latlongs :"+ res.rows.item(i).Coordinates); myLocationcoordinates[i] = res.rows.item(i).Coordinates; } }); }); } }

任何建议,

推荐答案

尝试简化了code。我认为这个问题是有一个为内部的为使用相同的迭代器的名称。

Try simplifying the code. I think the issue is having a for inside of a for with the same iterator name.

下面是我怎么会尝试这个例子:

Here is an example of how I would try this:

function sendCategoryDetailsNew(myLocation) { var myLocationcoordinates = new Array(); var db = window.sqlitePlugin.openDatabase({name: "MYDB"}); var locationsList = "\'" + myLocation.join("\',\'") + "\'"; db.transaction(function (tx) { tx.executeSql("SELECT Location, Coordinates FROM Locationlog WHERE Location IN (?)", locationsList, function(tx, res) { for (var i = 0; i < res.rows.length; i++) { console.log("Location : " + res.rows.item(i).Location + " Latlongs : " + res.rows.item(i).Coordinates); myLocationcoordinates[i] = res.rows.item(i).Coordinates; } }); } }

而不是通过两组值循环的,我编了 myLocation 数组,可以被用在在 SQL语句。这将返回为 myLocation 阵列中的所有结果,然后遍历它们进行处理。

Instead of looping through two sets of values, I have compiled the myLocation array into a string that can be used in an IN sql statement. This will return all results for the myLocation array and then iterate through them for processing.

这减少了大量的处理,也是在该函数创建失败的少点。

This cuts down a lot of processing and also creates less fail points in the function.

更多推荐

问题与循环SQLite中选择查询?

本文发布于:2023-10-26 14:51:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!