字段类型:
id = db.Column()
int类型:db.Integer
主键:primary_key=True
代表的是将这个字段设置为主键。
自增长:autoincrement=True
使得每当表中增加一项,id就自动加一
字符型:db.String()
需要指定最长的长度
是否为空:nullable=False
代表这个字段是否为空
文本类型:db.Text
模型需要继承自db.Model
,然后需要映射到表中的属性,必须写成id = db.Column()
创建数据库:create database db__demo1 charest utf8;
在mysql中使用数据库:use db_demo1;
显示创建的表单:show tables;
显示表的详细信息:desc article;
db1.py代码
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config
import pymysql
pymysql.install_as_MySQLdb()
app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)
class Article(db.Model):
__tablename__ = 'article'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
title = db.Column(db.String(100),nullable=False)
content = db.Column(db.Text,nullable=False)
db.create_all()
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()