1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flaskでmysqlからデータ取得し表示

Last updated at Posted at 2023-03-24

anaconda環境にFlask環境構築済みであることが前提です


1.pymysqlを組み込む

(flask-test) [xxx flask-test]$ conda install pymysql
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/xxx/anaconda3/envs/flask-test

  added / updated specs:
    - pymysql

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    pymysql-0.9.3              |           py37_0          83 KB
    ------------------------------------------------------------
                                           Total:          83 KB

The following NEW packages will be INSTALLED:

  pymysql            pkgs/main/linux-64::pymysql-0.9.3-py37_0

Proceed ([y]/n)? y

Downloading and Extracting Packages
pymysql-0.9.3        | 83 KB     | ################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

2.Flask環境に以下モジュールを組み込む

2-1.ファイル

(flask-test) [xxx flask-test]$ tree
.
├── db_mysql_get.py
└── templates
    └── dbmysql.html

2-2.db_mysql_get.py

import pymysql

from flask import Flask,render_template,request

#インスタンス生成
app = Flask(__name__)

def getConnection():
  return pymysql.connect(
    host='[hostname]',
    port=int(3306),
    db='[dbname]',
    user='[userid]',
    passwd='[password]',
    charset='utf8',
  )

@app.route('/dbmysql/')
def dbmysql():
  conn = getConnection()
  cur = conn.cursor()
  sql = "select"
  sql += " col1 "
  sql += ",col2 "
  sql += " from "
  sql += " table1 "
  cur.execute(sql)
  datalist = cur.fetchall()

  cur.close()
  conn.close()

  return render_template('dbmysql.html',datalist=datalist)

if __name__ == "__main__":
  #Webサーバ立ち上げ
  app.debug=True
  app.run(host='xxx.xxx.xxx.xxx')

2-3.dbmysql.html

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>db Mysql Data</title>
</head>
<body>
  <table border=1>
    <tr>
     <th>col1name</th>
     <th>col2name</th>
    </tr>
   {% for data in datalist %}
     <tr>
       <td>{{data[0]}}</td>
       <td>{{data[1]}}</td>
     </tr>
     </tr>
   {% endfor %}
   </table>  
</body>
</html>

3.実行

(flask-test) [xxx flask-test]$ python db_mysql_get.py
 * Serving Flask app "db_mysql_get" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://xxx.xxx.xxx.xxx:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 217-574-955

4.確認

####http://xxx.xxx.xxx.xxx:5000/dbmysql/
mysqlimage.PNG

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?