SQLite从联接表执行查询

时间:2018-12-09 09:20:13

标签: sqlite flask inner-join cs50

我正在为我的最终项目而苦苦挣扎。我需要加入2个表“ book”和“ idlist”,并执行index.html中的值。 这是python和html代码(也是idlist Table book Table)。 如果有人知道哪里出了错,我将不胜感激!

@app.route("/", methods=["GET", "POST"])
@login_required
def index():
    """Show reading list"""

    if request.method == "GET":
        # Execute list joining "book" and "idlist" tables
        list = db.execute("SELECT Title1, Status, LastUpdate, Author, Year, Country, Language FROM idlist INNER JOIN book on idlist.Title1=book.Title WHERE id=:id",
                          id=session["user_id"])
        # If the user has no list yet
        if not list:
            el = {'Title1': "No", 'Author': "No", 'Year': "No", 'Country': "No", 'Language': "No", 'Status': "No", 'LastUpdate': "No"}
            return render_template("index.html")
        else:
            return render_template("index.html")
    return render_template("index.html")

html应该执行联接表中的值

{% extends "layout.html" %}

{% block title %}
    Index
{% endblock %}
{% block main %}
        <table style="width:100%">
            <tr>
                <th>Title</th>
                <th>Author</th>
                <th>Year</th>
                <th>Country</th>
                <th>Language</th>
                <th>Status</th>
                <th>Last update</th>
            </tr>
                {% for el in list %}
                    <tr>
                        <td>
                            {{ el.Title1 }}
                        </td>
                        <td>
                            {{ el.Author }}
                        </td>
                        <td>
                            {{ el.Year }}
                        </td>
                        <td>
                            {{ el.Country }}
                        </td>
                        <td>
                            {{ el.Language }}
                        </td>
                        <td>
                            {{ el.Status }}
                        </td>
                        <td>
                            {{ el.LastUpdate }}
                        </td>
                    </tr>
                {% endfor %}
        </table>
{% endblock %}

这是我以用户ID 16登录时出现的错误 RuntimeError:“更新”附近:语法错误[SQL:在idlis上,从idlist INNER JOIN书中选择SELECT标题,状态,更新,作者,年份,国家/地区,语言 t.Title = book.Title WHERE id = 16']

1 个答案:

答案 0 :(得分:1)

我的程序应将2个表“ book”和“ idlist”连接起来并执行index.html中的值。 主要问题是错误地使用了“ render_template()”,更准确地说是我没有向其中传递任何数据。当我需要以html形式表示“列表”时,正确的用法是“返回render_template(“ index.html”,list = list)“

下面是解决方法:

@app.route("/", methods=["GET", "POST"])
@login_required
def index():
    """Show reading list"""

    if request.method == "GET":
        quote1 = db.execute("SELECT quote FROM quotes ORDER BY random() LIMIT 1")
        # Execute list joining "book" and "idlist" tables
        list = db.execute("SELECT Title1, Status, LastUpdate, Author, Year, Country, Language FROM idlist INNER JOIN book on idlist.Title1=book.Title WHERE id=:id",
                          id=session["user_id"])
        # If the user has no list yet
        if not list:
            el = {'Title1': "No", 'Author': "No", 'Year': "No", 'Country': "No", 'Language': "No", 'Status': "No", 'LastUpdate': "No"}
        return render_template("index.html", yourquote=quote1[0]["quote"])

    return render_template("index.html")

这是html格式:

{% extends "layout.html" %}

{% block title %}
    Index
{% endblock %}
{% block main %}
        <table style="width:100%">
            <tr>
                <th>Title</th>
                <th>Author</th>
                <th>Year</th>
                <th>Country</th>
                <th>Language</th>
                <th>Status</th>
                <th>Last update</th>
            </tr>
                {% for el in list %}
                    <tr>
                        <td>
                            {{ el.Title1 }}
                        </td>
                        <td>
                            {{ el.Author }}
                        </td>
                        <td>
                            {{ el.Year }}
                        </td>
                        <td>
                            {{ el.Country }}
                        </td>
                        <td>
                            {{ el.Language }}
                        </td>
                        <td>
                            {{ el.Status }}
                        </td>
                        <td>
                            {{ el.LastUpdate }}
                        </td>
                    </tr>
                {% endfor %}
        </table>
        <tr>
            <p> </p>

        </tr>
        <a class="card-header" href="/"><span class="blue">Inspire yourself</span></a>
         <tr>
            <p> </p>

        </tr>
        <a class="card-title"><span class="grey"><p>{{ yourquote }}</p></span></a>

{% endblock %}