【问题标题】:Python / SQL Not Displaying Connected Database to TablePython / SQL未将连接的数据库显示到表
【发布时间】:2018-12-12 17:26:52
【问题描述】:

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

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

更新.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 %}

Python.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)

@app.route('/update')
def update():
   c = get_db().cursor()
   # this just gets the data from the db
   c.execute('SELECT COMNAME FROM FLOWERS')
   comname_update = c.fetchall()
   c.execute('SELECT GENUS FROM FLOWERS')
   genus_update = c.fetchall()
   c.execute('SELECT SPECIES FROM FLOWERS')
   species_update = c.fetchall()
   zipped = zip(genus_update, species_update)
   return render_template("update.html", comname_update=comname_update, genus_update=genus_update, species_update=species_update, zipped=zipped)

@app.route('/profile/<name>')
def profile(name):
   return render_template("profile.html", name=name)


if __name__ == "__main__":
   app.run(debug=True)

已解决

解决方案

html代码:

{% for g, s, c in genus_flowers%}
                   <tr>
                       <td class="cell">{{g}}</td>
                       <td class="cell">{{s}}</td>
                       <td class="cell">{{c}}</td>

                   </tr>
{% endfor %}

python 代码:

@app.route('/update')
def update():
   c = get_db().cursor()
   # this just gets the data from the db

   c = get_db().cursor()
   c.execute('SELECT GENUS, SPECIES, COMNAME FROM FLOWERS')
   genus_flowers = c.fetchall()
   return render_template("update.html", genus_flowers=genus_flowers)

【问题讨论】:

  • 为什么不遍历zipped
  • @roganjosh 在我的 python 文件中我有'zipped = zip(genus_update,species_update)'这一行,但如果网站不中断,我无法成功实现它

标签: python html database sqlite html-table


【解决方案1】:

我知道在另一个 Python 网络框架 Django 中,您必须引用对象中的字段,而不仅仅是对象本身。因此,如果您执行 Select *,而不是 Select 'field':

@app.route('/update')
def update():
   c = get_db().cursor()
   # this just gets the data from the db
   c.execute('SELECT * FROM FLOWERS')
   flowers = c.fetchall()
   zipped = zip(genus_update, species_update)
   return render_template("update.html", flowers=flowers, zipped=zipped)

然后您可以执行以下操作:

<!--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 f in flowers %}
                   <tr>
                       <td class="cell">{{ f.genus }}</td>
                       <td class="cell">{{ f.species }}</td>
                       <td class="cell">{{ f.comname }}</td>
                   </tr>
                   {% endfor %}
               </table>
           </div>
           <div class="col">
           </div>
       </div>
   </div>

【讨论】:

  • 这行不通,因为没有键名。如果您的查询返回字典,f.genus 将起作用,但它没有,它返回一个元组列表。您必须使用索引,而不是名称
  • 确切的代码不起作用,但让我找到了解决方案。谢谢!
【解决方案2】:

我不确定这里到底会发生什么,但由于我之前处于这种情况,我建议您先测试数据是否是从数据库中获取的,而不是直接检查 Flask 所在的部分呈现它。确保也存在正在传递的查询的数据。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多