pythonでMYSQLからカラムを取得し、その結果を
HTMLで表示をさせるプログラムを作成中です。
上記自体は、実現できたのですが、表示される結果が
- ('あ', 'b', datetime.date(2022, 5, 22))
- ('c', 'd', datetime.date(2022, 5, 23))
- ('gggg', 'bb', datetime.date(2022, 5, 24))
- (''gg, 'んんんんn', datetime.date(2022, 5, 25))
- ('b', 'r', datetime.date(2022, 5, 26))
- ('fff', 'rrrr', datetime.date(2022, 5, 27))
- ('hh', 'ええ', datetime.date(2022, 5, 28))
という感じで非常に見た目が悪いのです。
実現したいこと
- あ b 5.22
- c d 5.23
- ええ おお 5.24
という風にカラム1とカラム2の文字の間隔の確保・日付をmm.ddにしたいです。
色々調べてみたのですが、クエリー結果の表示方法を変える方法を見つけられませんでした。
該当のソースコード
python
1#!/usr/bin/python3 2 3# -- coding: utf-8 -- 4 5from flask import Flask,render_template 6import mysql.connector as maya 7 8app = Flask( __name__ ) 9 10con = maya.connect( 11 database = 'maya', 12 user = 'root', 13 password = 'mta969696' , 14 host = 'localhost', 15 port = '3306',) 16 17# カーソルを取得して、処理終了時には自動でカーソルをcloseする。 18with con.cursor() as cursor: 19 20 21 # SQLの実行 22 sql = "SELECT * FROM c_data where date between\ 23 date_add( now(), interval -1 day ) and\ 24 date_add( now(), interval 6 day ) " 25 cursor.execute(sql) 26 27 # SQLの実行結果を取得 28 menu_s = cursor.fetchall() 29 30 cursor.close() 31 con.close() 32 33@app.route("/") 34def select_sql(): 35 return render_template('index.html', menu_s = menu_s )
MYSQL
データベース名 c_data
テーブル名 maya
カラム1 ryouri_1 text型
カラム2 ryouri_2 text型
カラム3 date date型
HTML
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7</head> 8<body> 9 {% block content %} 10 <h3>メニュー</h3> 11 <ul> 12 {% for item in menu_s %} 13 <li>{{ item }}</li> 14 {% endfor %} 15 </ul> 16 {% endblock %} 17 18 19</body> 20</html>
補足情報(FW/ツールのバージョンなど)
OS WINDOWS10
python3.9.12
pip list
Package Version
click 8.1.3
colorama 0.4.4
Flask 2.1.2
importlib-metadata 4.11.4
itsdangerous 2.1.2
MarkupSafe 2.1.1
mysql 0.0.3
mysql-connector 2.2.9
mysql-connector-python 8.0.29
mysqlclient 2.1.0
pip 22.0.4
protobuf 3.20.1
setuptools 58.1.0
Werkzeug 2.1.2
zipp 3.8.0