【问题标题】:How to get only the value from the SQL query output without parentheses如何仅从 SQL 查询输出中获取不带括号的值
【发布时间】:2020-05-18 15:05:35
【问题描述】:

这是我在烧瓶应用程序中的代码

from flask import Flask, render_template
from flask_mysqldb import MySQL
import MySQLdb.cursors

app = Flask(__name__)

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASWPRD'] = ''
app.config['MYSQL_DB'] = 'customer'

mysql = MySQL(app)

@app.route('/')
def index():
     cur = mysql.connection.cursor()
     cur.execute("SELECT City FROM customer")
     data = cur.fetchall()
     cur.close()
   return render_template ("index.html", name=data)
if __name__ == "__main__":
   app.run(debug=True)

返回这个

你好世界 (('DHK',), ('KHL',), ('RAJ',), ('COM',))

但我想这样展示 DHK, KHL,......

我怎样才能得到它并在 HTML 模板中使用它

更新

这里是 index.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
</head>
<body>
    <head>Hello World</head>
    
    <h1>{{ name }}</h1>
</body>
</html>

【问题讨论】:

  • 能否提供 index.html 模板?我相信答案会在那里
  • 我在“UPDATE”下添加了文件

标签: mysql sql sql-query-store sqlresultsetmapping flask-mysql


【解决方案1】:

你可以试试这个吗?我认为您需要循环传递给模板的这个值:

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
  </head>

  <body>

    <head>Hello World</head>

    {% for message in name %}
      {% for item in message %}
        {{ item }},
      {% endfor %}
    {% endfor %}
  </body>

</html>

【讨论】:

  • 谢谢。但它显示这个 >>> Hello World ('DHK',), ('KHL',), ('RAJ',), ('COM',),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 2015-09-30
  • 2020-07-31
相关资源
最近更新 更多