python
from flask import Flask, render_template
import pymysql
app = Flask(__name__)
def getConnection():
return pymysql.connect(
host='localhost',
db='mydb',
user='root',
password='',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor
)
@app.route('/')
def select_sql():
connection = getConnection()
message = "Hello world"
sql = "SELECT * FROM players"
cursor = connection.cursor()
cursor.execute(sql)
players = cursor.fetchall()
cursor.close()
connection.close()
return render_template('view.html', message = message, players = players)
--Declared to edit with pymysql
--Select the database information to edit with .connect ()
--Enter SQL command in the database selected with def select_sql
python
@app.route('/')
def select_sql():
connection = getConnection()
message = "Hello world"
cursor = connection.cursor()
sql = "INSERT INTO players (name, level, job_id) VALUES ('Kirishima No. 1', 1, 1)"
cursor.execute(sql)
connection.commit()
sql = "SELECT * FROM players"
cursor.execute(sql)
players = cursor.fetchall()
cursor.close()
connection.close()
return render_template('view.html', message = message, players = players)
--The basics are the same
--It is better to do cursor = connection.cursor ()
first so that you can refer to cursor
.
--sql =" INSERT INTO players (name, level, job_id) VALUES ('Kirishima No. 1', 1, 1) "
, select table with ʻINSERT INTO