将 Javascript 变量从一个函数传递到另一个函数以在 sqlite 查询中使用

发布于 2024-11-07 06:30:55 字数 2052 浏览 0 评论 0原文

我正在尝试编写一个 Javascript 函数,该函数从 sqlite 数据库获取一组唯一的 id然后将它们传递给另一个函数,其中 ids 可以在另一个 sql 查询中使用,它们也构成动态创建列表的一部分。

我已成功将 ids row['id'] 传递给数组变量 window.symp[i]。但我无法在下面的第二个函数中正确访问它们,第二个函数正确使用 ids 来创建动态 html,但传递给 sqlite 查询的变量要么失败,要么在创建的所有列表项中具有相同的值。任何帮助将不胜感激 - 我已包含以下两个功能:

function showContent() {
    db.transaction(function (tx) {
        tx.executeSql("SELECT id, notes FROM webkit WHERE notes LIKE 'A%'", [], function (tx, result) {
            var notesanode = document.getElementById('notesa');
            notesanode.innerHTML = "";

            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                window.symp[i] = i;
                window.symp[i] = row['id'];
                var noteadiv = document.createElement('div');
                noteadiv.innerHTML = '<li class=\"arrow\"><a id=\"0\" onClick=\"showSymptoms()\" href=\"#symptoms\">' + row['notes'] + " " + row['id'] + '</a></li>';
                notesanode.appendChild(noteadiv);

            }

        }, function (tx, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
}

function showSymptoms() {
    db.transaction(function (tx) {
        tx.executeSql("SELECT sid, symptom FROM slinks WHERE id LIKE ('" + symp + "')", [], function (tx, result) {
            var symptomnode = document.getElementById('symptomid');
            symptomnode.innerHTML = "";

            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                var symptomdiv = document.createElement('div');
                symptomdiv.innerHTML = '<p><label> <input type=checkbox>' + row['symptom'] + '</label></p>';
                symptomnode.appendChild(symptomdiv);
            }

        }, function (tx, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
}

I am trying to write a Javascript function that obtains an array of unique ids from an sqlite database & then passes them to be used in another function where the ids can be used in another sql query, they also form part of a dynamically created list.

I have managed to pass the ids row['id'] to the array variable window.symp[i]. But I have not been able to access them correctly in the second function below, the second function correctly uses the ids to create the dynamic html but the variable passed to the sqlite query either fails or is the same value in all the list items created. Any help would be greatly appreciated - I have included both functions below:

function showContent() {
    db.transaction(function (tx) {
        tx.executeSql("SELECT id, notes FROM webkit WHERE notes LIKE 'A%'", [], function (tx, result) {
            var notesanode = document.getElementById('notesa');
            notesanode.innerHTML = "";

            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                window.symp[i] = i;
                window.symp[i] = row['id'];
                var noteadiv = document.createElement('div');
                noteadiv.innerHTML = '<li class=\"arrow\"><a id=\"0\" onClick=\"showSymptoms()\" href=\"#symptoms\">' + row['notes'] + " " + row['id'] + '</a></li>';
                notesanode.appendChild(noteadiv);

            }

        }, function (tx, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
}

function showSymptoms() {
    db.transaction(function (tx) {
        tx.executeSql("SELECT sid, symptom FROM slinks WHERE id LIKE ('" + symp + "')", [], function (tx, result) {
            var symptomnode = document.getElementById('symptomid');
            symptomnode.innerHTML = "";

            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                var symptomdiv = document.createElement('div');
                symptomdiv.innerHTML = '<p><label> <input type=checkbox>' + row['symptom'] + '</label></p>';
                symptomnode.appendChild(symptomdiv);
            }

        }, function (tx, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-11-14 06:30:55

我看到两件事:

主要问题是你需要什么,

"SELECT sid, symptom FROM slinks WHERE id LIKE ('" + window.symp.join(',') + "')"

而不是你现有的东西。

第二个问题是您

window.symp[i] = i;
window.symp[i] = row['id'];

可以继续删除 window.symp[i] = i; 因为它会立即被后面的行覆盖。

I see two things:

The main issue is that you need

"SELECT sid, symptom FROM slinks WHERE id LIKE ('" + window.symp.join(',') + "')"

instead of what you have up there.

The second issue is that you have

window.symp[i] = i;
window.symp[i] = row['id'];

you can just go ahead and remove window.symp[i] = i; because it gets immediately overwritten by the line that follows it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文