更新时间:2022-09-22 09:53:03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#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个表,表里也已经有内容数据了。
本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/1973767如需转载请自行联系原作者
world77