#coding=UTF8 #!/usr/bin/env python from flask import Flask from flask_sqlalchemy import SQLAlchemy app=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 ‘<User : %r>‘ % 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 ‘<User : %r>‘ % 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个表,表里也已经有内容数据了。
本文出自 “你是路人甲还是霍元甲” 博客,请务必保留此出处http://world77.blog.51cto.com/414605/1973767
原文地址:http://world77.blog.51cto.com/414605/1973767