#coding=UTF8#!/usr/bin/env pythonfrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp=Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://qhz:qhz123@10.10.10.57:3560/study'app.config['SECRET_KEY'] = 'SQLalPython'db = SQLAlchemy(app)class User(db.Model): __tablename__ = 'BlogUser' id=db.Column(db.Integer,primary_key=True) username=db.Column(db.String(50),unique=True) password=db.Column(db.String(100)) email=db.Column(db.String(100),unique=True) def __init__(self,username,password,email): self.username = username self.password = password self.email = email def __repr__(self): return '' % self.username class Admin(db.Model): __tablename__ = 'BlogAdmin' id=db.Column(db.Integer,primary_key=True) username=db.Column(db.String(50),unique=True) password=db.Column(db.String(100)) email=db.Column(db.String(100),unique=True) def __init__(self,username,password,email): self.username = username self.password = password self.email = email def __repr__(self): return ' ' % self.username db.create_all()a=User(username='jack',password='jack123',email='world1@163.com')b=User(username='mike',password='mike123',email='world7@163.com')c=Admin(username='qhz',password='qhz1123',email='world2@163.com')try: db.session.add(a) db.session.add(b) db.session.add(c) db.session.commit()except StandardError,e: db.session.rollback() print e
运行之后到数据库看时,已经自动生成了2个表,表里也已经有内容数据了。