The flask framework of python connects to the mysql database

The database used in the development of "flaskweb" is sqlite. We commonly use databases such as mysql. The following describes how to connect to mysql under flask.

  1. Drive ready.

pip install flask-sqlalchemy
pip install mysql-python
is mainly these two drivers. There are more installation methods, so I won't describe them too much.

  1. Database settings

First import the package
from flask_sqlalchemy import SQLAlchemy
and then configure the database

app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:123@localhost:3306/test' 
#这里登陆的是root用户,要填上自己的密码,MySQL的默认端口是3306,填上之前创建的数据库名test
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True 
#设置这一项是每次请求结束后都会自动提交数据库中的变动
  1. Define the model and establish the relationship: in hello.py
class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name


class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username
  1. Run hello.py to
    run hello.py, you can create a database, add data and other operations in the console.
    Complete hello.py code
# -*- coding: utf-8 -*-
_author_ = 'Pylar'
from flask import Flask,render_template,session,redirect,url_for,flash
from flask_script import Manager
from flask_bootstrap import Bootstrap
from datetime import datetime
from flask_moment import Moment
from flask_wtf import Form
from wtforms import StringField,SubmitField
from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SECRET_KEY'] = 'haha'
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:123@localhost:3306/test' #这里登陆的是root用户,要填上自己的密码,MySQL的默认端口是3306,填上之前创建的数据库名text1
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True #设置这一项是每次请求结束后都会自动提交数据库中的变动


bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app) #实例化
# manager = Manager(app)

class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name


class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username



class NameForm(Form):
    name = StringField('What is your name?',validators=[Required()])
    submit = SubmitField('Submit')

@app.errorhandler(404)
def page_not_found(e):
    render_template('404.html'),404

@app.errorhandler(500)
def internal_server_error(e):
    render_template('500.html'),500

@app.route('/',methods = ['GET','POST'])
def index():
    name = None
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        return redirect(url_for('index'))
    return render_template('index.html',form=form,name=session.get('name'))

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


if __name__ == '__main__':
   app.run(debug=True)
  1. success
    create database
    success

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325418628&siteId=291194637