0

亲爱的 Stackoverflow 用户,

我们创建的数据库如下:

function populateDB(tx) {
    tx.executeSql('DROP TABLE IF EXISTS Gerechten');
    tx.executeSql('CREATE TABLE IF NOT EXISTS Gerechten (id INTEGER PRIMARY KEY AUTOINCREMENT, Cat TEXT NOT NULL, Name TEXT NOT NULL, Desc TEXT NOT NULL)');
    tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Voorgerecht", "Tomatensoep", "Heerlijke romige tomatensoep")');
    tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Hoofdgerecht", "Pizza", "Kaas en tomaat")');
    tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Voorgerecht", "Groentensoep", "Goed gevulde groentensoep")');
    tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Voorgerecht", "Kippensoep", "Kippensoep met stukjes kip")');
    tx.executeSql('INSERT INTO Gerechten(Cat,Name,Desc) VALUES ("Hoofdgerecht", "Biefstuk", "Biefstuk, wordt geserveerd met patat of rijst")');
}

我有一个Javascript循环的问题,代码如下:

function queryDB(tx){
    tx.executeSql('SELECT DISTINCT "Cat" FROM Gerechten',[],function (tx, results) {
        var len = results.rows.length, i;
        msg = "<p>Found rows: " + len + "</p>";
        document.write(msg + '<br/>');
        for (i = 0; i < len; i++){
            document.write(results.rows.item(i).Cat + '<br/>');
            tx.executeSql('SELECT * FROM Gerechten WHERE "Cat" ="'+results.rows.item(i).Cat+'"',[],function (tx, results2) {
                var len2 = results2.rows.length, y;
                msg = "<p>Found rows: " + len2 + "</p>";
                document.write(msg + '<br/>');
                for (y = 0; y < len2; y++){
                    document.write(results2.rows.item(y).Name + '<br/>');
                }
            }, null);
        }
    }, null);
}

代码的问题是首先输出类别,然后显示名称(以正确的顺序):

Voorgerecht
Hoofdgerecht
  Tomatensoep
  Groentensoep
  Kippensoep
  Pizza
  Biefstuk

正确的顺序应该是:

Voorgerecht
  Tomatensoep
  Groentensoep
  Kippensoep
Hoofdgerecht
  Pizza
  Biefstuk

有谁知道如何解决这个问题?

亲切的问候,

丹尼斯

4

2 回答 2

1

您的问题是executeSql异步执行您的函数(其最后一个参数),因此您的外部循环可能会在内部循环的查询完成执行之前打印所有类别。

您应该只使用一个查询,以便您可以按正确的顺序处理所有结果。尝试这样的事情:

SELECT * FROM Gerechten ORDER BY Cat

并在列中的值更改时打印类别Cat(请参阅 Jose 的答案)。

于 2012-12-03T20:59:49.337 回答
0

老实说,您不需要多次查询数据库,假设:

选择 * 从 Gerechten

并且您的结果包含以下内容,您可以执行以下操作并且仍然产生相同的结果。显然,您需要实现它以匹配您的 web sql 数据库 api,但逻辑应该保持不变。

var results = [
    {
        "Cat" : "Voorgerecht",
        "Name" : "Tomatensoep",
        "Desc" : "Heerlijke romige tomatensoep"
    },{
        "Cat" : "Hoofdgerecht",
        "Name" : "Pizza",
        "Desc" : "Kaas en tomaat"
    },{
        "Cat" : "Voorgerecht",
        "Name" : "Groentensoep",
        "Desc" : "Goed gevulde groentensoep"
    },{
        "Cat" : "Voorgerecht",
        "Name" : "Kippensoep",
        "Desc" : "Kippensoep met stukjes kip"
    },{
        "Cat" : "Hoofdgerecht",
        "Name" : "Biefstuk",
        "Desc" : "wordt geserveerd met patat of rijst"
    }
];

//sorts by the Category
results.sort(compare);
var initial = "";

for(var y = 0; results.length; y++) {
    if(initial != results[y].Cat){
        initial = results[y].Cat;
         document.write(initial + "<br/>");
    }

    document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + results[y].Name + "<br/>");
}


function compare(a,b) {
    if (a.Cat < b.Cat)
        return 1;
    if (a.Cat > b.Cat)
        return -1;    
    else
      return 0;
}

输出

Voorgerecht
     Tomatensoep
     Groentensoep
     Kippensoep
Hoofdgerecht
     Pizza
     Biefstuk
于 2012-12-03T18:49:09.297 回答