第一步app.py:
# coding=utf-8
import urllib
from flask import Flask
from werkzeug.routing import BaseConverter
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI']="mysql://root:abc31127897@127.0.0.1:3306/pytest"
db = SQLAlchemy(app)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0',port=9000)
第二步models.py:
import MySQLdb
from pytest import db
class Users(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
password = db.Column(db.String)
def __init__(self, id, name, password):
self.id = id
self.name = name
self.password = password
第三步manager.py:
# encoding=utf-8
from flask_script import Manager
from pytest import app,db
from models import Users
manager = Manager(app)
@manager.command
def save():
user = Users(4,'paul','12345678')
db.session.add(user)
db.session.commit()
@manager.command
def query_all():
users = Users.query.all()
for u in users:
print u
if __name__=='__main__':
manager.run()
完成!