Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从flask调用的sqlite db只返回变量,不返回值_Python_Html_Sqlite_Flask - Fatal编程技术网

Python 从flask调用的sqlite db只返回变量,不返回值

Python 从flask调用的sqlite db只返回变量,不返回值,python,html,sqlite,flask,Python,Html,Sqlite,Flask,我有一个flask应用程序,可以查询sqlite数据库: @app.route('/<subject_id>') def subject_id_lookup(subject_id): entries = query_db('select visitdt, cvnotes from exam where id = ?', [subject_id], one=True) return render_template('sho

我有一个flask应用程序,可以查询sqlite数据库:

@app.route('/<subject_id>')
def subject_id_lookup(subject_id):
    entries = query_db('select visitdt, cvnotes from exam where id = ?',
                        [subject_id], one=True)
    return render_template('show_results.html', entries = entries)
最后是我的show_results.html文件:

{% extends "layout.html" %}
{% block body %}
    <ul class=entries>
        {% for entry in entries %}
        <li><h2>{{ entry }}</h2>
        <br>
        {% else %}
        <li><em>No entry here</em>
        {% endfor %}
    </ul>
    {% endblock %}
{%extends“layout.html”%}
{%block body%}
    {entries%%中的条目的百分比}
  • {{entry}}
    {%else%}
  • 这里不准入内 {%endfor%}
{%endblock%}

查询运行正常,但除了变量名
visitdt
cvnotes
之外,什么也不返回。当我将上面的行更改为
  • {{entry.cvnotes}}
  • 时,它不返回任何内容。如何修改查询以显示我的
    subject\u id\u lookup()
    函数的结果?

    问题在于
    query\u db
    根据您指定的是
    one=True
    还是
    one=False
    返回不同的内容

    >>> query_db(your_query, [some_id], one=True)
    {visittd: "a value", cvnotes: "some notes"}
    
    >>> query_db(your_query, [some_id], one=False)
    [{visittd: "a value", cvnotes: "some notes"}] # Note the wrapping list
    
    当您在字典上枚举时,结果是字典中的键-当您在列表上枚举时,结果是列表中的条目

    >>> for thing in query_db(your_query, [some_id], one=True):
    ...    print thing
    visitdt
    cvnotes
    
    >>> for thing in query_db(your_query, [some_id], one=False):
    ...    print thing
    {visittd: "a value", cvnotes: "some notes"}
    
    如果您想使用相同的模板,并且知道一个
    id
    只返回一个值(或者如果您可以处理多个值),只需删除
    subject\u id\u lookup
    中的
    one=True
    关键字参数即可<代码>条目将是一个包含键的字典列表
    visitdt
    cvnotes
    -当您在模板中对其进行迭代时,每个条目将成为一个结果字典(而不是单个结果字典中的键),并且
    {{entry.cvnotes}
    将起作用

    >>> for thing in query_db(your_query, [some_id], one=True):
    ...    print thing
    visitdt
    cvnotes
    
    >>> for thing in query_db(your_query, [some_id], one=False):
    ...    print thing
    {visittd: "a value", cvnotes: "some notes"}