flask 操作mysql的两种方式-sqlalchemy操作
生活随笔
收集整理的這篇文章主要介紹了
flask 操作mysql的两种方式-sqlalchemy操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
flask 操作mysql的兩種方式-sqlalchemy操作二、ORM sqlalchemy操作#coding=utf-8
# model.pyfrom app import dbclass User(db.Model):user_id = db.Column(db.Integer, primary_key=True)user_name = db.Column(db.String)def __init__(self, user_id, user_name):self.user_id = user_idself.user_name = user_namedef __str__(self):return "id:{id}-name:{name}".format(id = self.user_id, name = self.user_name)#coding=utf-8
# app.pyfrom flask import Flask
from flask.ext.sqlalchemy import SQLAlchemyapp = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:root@123@localhost:3306/jikexueyuan"db = SQLAlchemy(app)@app.route("/")
def hello_world():return "hello world"if __name__ == "__main__":app.run()#coding=utf-8
# manage.pyfrom flask_script import Manager
from app import app, db
from model import Usermanager = Manager(app)@manager.command
def save():user = User(4, "curry")db.session.add(user)db.session.commit()@manager.command
def query_all():users = User.query.all()for user in users:print userif __name__ == "__main__":manager.run()#表結(jié)構(gòu)
CREATE DATABASE IF NOT EXISTS `jikexueyuan` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;CREATE TABLE `jikexueyuan`.`user`(`user_id` INT NOT NULL,`user_name` VARCHAR(45) NULL,PRIMARY KEY (`user_id`))
ENGINE = INNODB;
?
轉(zhuǎn)載于:https://www.cnblogs.com/themost/p/8796196.html
總結(jié)
以上是生活随笔為你收集整理的flask 操作mysql的两种方式-sqlalchemy操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bootstrap 表格
- 下一篇: 分布式服务动态上下线感知