【发布时间】:2019-05-10 12:57:27
【问题描述】:
我正在尝试使用 Electron 和一个名为 SQlite 的数据库构建一个应用程序。我想从多个表中创建一个标题和字幕列表。它需要看起来像这样:
**Titlename 1**
- Subname 1
- Subname 2
- Subname 3
**Titlename 2**
- Subname 4
- Subname 5
- Subname 6
这是我的代码:
db.serialize(() => {
var rows = document.getElementById("database");
db.each("SELECT DISTINCT titlename, Subname FROM sqlitedatabase ORDER BY Titleorder", function(err,row) {
// The results from the database (the titlename) are put into <p id="chaptertitle">
var item = document.createElement("p");
item.setAttribute("id", "chaptertitle");
item.textContent = row.chapname;
// The results of the subchapter column go into a second variable:
var item2 = document.createElement("p");
item2.setAttribute("id", "subchaptertitle");
item2.textContent = row.Subchapter;
// Then both <p>-elements are put into the <div id='database'>
rows.appendChild(item);
rows.appendChild(item2);
});
});
// and we close the database
db.close();
结果和我想的不太一样:
<div id="database">
<p id="chaptertitle">Titlename 1</p>
<p id="subchaptertitle">Subname 1</p>
<p id="chaptertitle">Titlename 1</p>
<p id="subchaptertitle">Subname 2</p>
</div>
我该如何解决这个问题?我想我需要使用某种循环,但我不知道该怎么做。类似'foreach chaptertitle 选择相应的字幕'。你能帮帮我吗?
【问题讨论】:
-
有人吗?有什么我应该看的文档或方向吗?
-
不清楚你是问如何在查询中加入多个表,还是在js中chaptertitle只有在变化时才处理。