赞
踩
《flaskweb》开发里用的数据库是sqlite ,我们常用的是mysql等数据库,下面介绍下如何在flask下连接mysql。
pip install flask-sqlalchemy
pip install mysql-python
主要是这两个驱动。安装方法还有比较多,就不过多叙述了。
首先导包
from flask_sqlalchemy import SQLAlchemy
然后配置数据库
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:123@localhost:3306/test'
#这里登陆的是root用户,要填上自己的密码,MySQL的默认端口是3306,填上之前创建的数据库名test
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
#设置这一项是每次请求结束后都会自动提交数据库中的变动
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
# -*- 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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。