Python / SQL 不显示连接的数据库到表

我有一个连接的 SQL 花卉数据库(属、种、名称),其中填充了一个只显示该信息的表。我只能让属列正确填充,但不能正确填充物种/名称列。


工作代码 - 正确显示一列:


更新.html


<!--this is for the table -->

   <div class="container">

       <div class="row">

           <div class="col">

               <table id="table" border="1">

                   <tr>

                       <th class="cell">Genus</th>

                       <th class="cell">Species</th>

                       <th class="cell">Comname</th>

                   </tr>

                   <!--the next line with the python code works as long as you only want the genus information-->

                   {% for g in genus_update %}

                   <tr>

                       <td class="cell">{{g}}</td>

                       <!--<td class="cell">{{g}}</td>-->

                       <!--<td class="cell">{{c}}</td>-->

                   </tr>

                   {% endfor %}

               </table>

           </div>

           <div class="col">

               <!--the right column so that everything is lined up on the left side-->

           </div>

       </div>

   </div>

尝试为其他人使用 for 循环会破坏页面(不确定为什么):


{% for s in species_update %}

  <tr>

    <td class="cell">{{s}}</td>

  </tr>

{% endfor %}


{% for c in comname_update %}

  <tr>

    <td class="cell">{{c}}</td>

  </tr>

{% endfor %}

蟒蛇.py:


from flask import Flask, render_template, request, g

import sqlite3


app = Flask (__name__)


# conn = sqlite3.connect('flowers.db')

# c = conn.cursor()


DATABASE = 'flowers.db'

def get_db():

   db = getattr(g, '_database', None)

   if db is None:

       db = g._database = sqlite3.connect(DATABASE)

   return db


@app.teardown_appcontext

def close_connection(exception):

   db = getattr(g, '_database', None)

   if db is not None:

       db.close()


@app.route('/')

def index():

   c = get_db().cursor()

   c.execute('SELECT COMNAME FROM FLOWERS')

   all_flowers = c.fetchall()

   return render_template("index.html", all_flowers=all_flowers)



泛舟湖上清波郎朗
浏览 373回答 3
3回答

慕哥9229398

解决了解决方案html代码:{% for g, s, c in genus_flowers%}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td class="cell">{{g}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td class="cell">{{s}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td class="cell">{{c}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>{% endfor %}蟒蛇代码:@app.route('/update')def update():&nbsp; &nbsp;c = get_db().cursor()&nbsp; &nbsp;# this just gets the data from the db&nbsp; &nbsp;c = get_db().cursor()&nbsp; &nbsp;c.execute('SELECT GENUS, SPECIES, COMNAME FROM FLOWERS')&nbsp; &nbsp;genus_flowers = c.fetchall()&nbsp; &nbsp;return render_template("update.html", genus_flowers=genus_flowers)

狐的传说

我知道在 Django 中,python 的另一个 web 框架,你必须引用对象中的字段,而不仅仅是对象本身。因此,如果您执行 Select *,而不是 Select 'field':@app.route('/update')def update():&nbsp; &nbsp;c = get_db().cursor()&nbsp; &nbsp;# this just gets the data from the db&nbsp; &nbsp;c.execute('SELECT * FROM FLOWERS')&nbsp; &nbsp;flowers = c.fetchall()&nbsp; &nbsp;zipped = zip(genus_update, species_update)&nbsp; &nbsp;return render_template("update.html", flowers=flowers, zipped=zipped)然后,您可以执行以下操作:<!--this is for the table -->&nbsp; &nbsp;<div class="container">&nbsp; &nbsp; &nbsp; &nbsp;<div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<table id="table" border="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th class="cell">Genus</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th class="cell">Species</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th class="cell">Comname</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<!--the next line with the python code works as long as you only want the genus information-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% for f in flowers %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td class="cell">{{ f.genus }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td class="cell">{{ f.species }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td class="cell">{{ f.comname }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="col">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp;</div>

拉丁的传说

我不确定这里究竟会发生什么,但由于我处于这种情况之前,我建议您先测试是否从数据库中获取数据,而不是直接检查 Flask 呈现它的部分。确保传递的查询的数据也存在。希望这将有助于进展。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python