zoukankan      html  css  js  c++  java
  • 20,使用Flask_SQLAlchemy连接多个数据库

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    
    
    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    
    app = Flask(__name__)
    
    # 配置多个数据库连接
    SQLALCHEMY_BINDS = {
        'users': 'sqlite:///users.db',
        'appmeta': 'sqlite:///appmeta.db'
    }
    
    app.config['SQLALCHEMY_DATABASE_URI'] =  'sqlite:///test.db' # 默认数据库引擎
    app.config['SQLALCHEMY_BINDS'] = SQLALCHEMY_BINDS
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    
    db = SQLAlchemy(app)
    
    
    class News(db.Model):
        __tablename__ = 'news' # 未设置__bind_key__,则采用默认的数据库引擎
      
    __bind_key__ = 'test2' # 已设置__bind_key__,则采用设置的数据库引擎  
    
        id = db.Column(db.Integer, primary_key=True)
        news_title = db.Column(db.String(80), unique=True)
        news_content = db.Column(db.String(120), unique=True)
    
        def __init__(self, news_title, news_content):
            self.news_title = news_title
            self.news_content = news_content
    
        def __repr__(self):
            return '<news_title %r>' % self.news_title
    
    class User(db.Model):
        __tablename__ = 'user'
    
        id = db.Column(db.Integer, primary_key=True)
        username = db.Column(db.String(80), unique=True)
        email = db.Column(db.String(120), unique=True)
    
        def __init__(self, username, email):
            self.username = username
            self.email = email
    
        def __repr__(self):
            return '<User %r>' % self.username
    
    class Article(db.Model):
        __bind_key__ = 'test1'
        __tablename__ = 'article'
    
        id = db.Column(db.Integer, primary_key=True)
        title = db.Column(db.String(80), unique=True)
        content = db.Column(db.String(120), unique=True)
    
        def __init__(self, title, content):
            self.title = title
            self.content = content
    
        def __repr__(self):
            return '<Title %r>' % self.title
    
    
    db.create_all() # 未指定bind,则使用默认的数据库引擎
    db.create_all(bind='test1') # 指定bind,则使用指定的数据库引擎
    db.create_all(bind='test2')
    
    news = News('aa','aaaaaaa') # 自动关联到相对应的ORM模型,进而使用相关联的数据库引擎
    db.session.add(news) # 插入一条数据
    db.session.commit()
    
    admin = User('admin', 'admin@example.com')
    guest = User('guest', 'guest@example.com')
    db.session.add_all([admin,guest]) # 插入多条数据
    db.session.commit()
    
    title = Article('title1', 'content1')
    db.session.add(title)
    db.session.commit()
    
    """
    执行该文件,会自动生成三个数据库文件:test1.db,users.db,test2.db
    每个数据库中插叙的有相对应的数据
    """
     
  • 相关阅读:
    第一个spring MVC
    AOP切点相关
    设计模式详解
    Spring基本原理模拟(IoC部分)
    AOP常用注解
    Integer.valueOf与Integer.parseInt的小疑惑
    IE10与IMG图片PNG显示不了 WP中的WebBrowser中无法查看PNG格式的图片
    去除Coding4Fun中MessagePrompt的边框(Border)
    windows phone 中的TextBlock的一些特性(TextWrapping,TextWrapping)
    九度 找出两个只出现了一次的数字
  • 原文地址:https://www.cnblogs.com/feifeifeisir/p/12095521.html
Copyright © 2011-2022 走看看