目录

前文列表

用 Flask 来写个轻博客 (1) — 创建项目

用 Flask 来写个轻博客 (2) — Hello World!

扩展阅读

SQLAlchemy_定义(一对一/一对多/多对多)关系

前言

大多数的应用程序在开发之前都需要先进行数据库设计这一环节,所以本篇就先来记录在 Flask 中如何使用 Models,也就是 MVC 模式中的 M 。

Models 模型

模型 就是对 数据抽象 并且提供一种 统一的通用访问接口 方式。

在大多数的 Web 应用中,都会将数据存储在 关系型数据库 中,EG. Mysql/Oracle/Postgres 。(当然现在也越来越多的在使用 非关系型数据库,但本篇只对前者做记录。) 这就会出现一个问题,怎样才能让关系型数据在面向对象编程结构的应用程序中得到更好的契合? 问了解决这个问题,在 Web 应用程序开发中引入了 Models 的概念 —— 将关系型数据转化为一个对象类型

SQLAlchemy

SQLAlchemy 是一个 Python 包,其最底层包装了对数据库进入操作的统一接口,然后在最上层提供了对象关系映射(ORM)的实现。

ORM 是在基于不同的数据结构和不同的系统类型之间进行传递和转换数据的计数。简而言之,SQLAlchemy-ORM 可以把大量的不同类型的数据库中的数据,转换成 Python 对象的集合。也就是说,SQLAlchemy-ORM 可以将对这些数据对象的操作转化为对数据库的操作。

Flask 为我们提供了 Flask SQLAlchemy,其实就是在 SQLAlchemy 上提供了一层包装,让 SQLAlchemy 可以结合 Flask 的一些特性来使用。

安装 SQLAlchemy

(env) [root@flask-dev JmilkFan-s-Blog]# pip install flask-sqlalchemy
(env) [root@flask-dev JmilkFan-s-Blog]# pip freeze > requirements.txt

flask-sqlalchemy 默认支持 SQLite,但是这里我们会使用 Mysql,所以还需要安装 SQLAlchemy 和 Mysql 之间的连接器。

(env) [root@flask-dev JmilkFan-s-Blog]# pip install PyMySQL
(env) [root@flask-dev JmilkFan-s-Blog]# pip freeze > requirements.txt

安装 Mysql

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
service mysqld restart

初始化 MySQL 并设置登陆密码:

[root@flask-dev JmilkFan-s-Blog]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here. Enter current password for root (enter for none):
OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation. Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success! By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment. Remove anonymous users? [Y/n]
... Success! Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n]
... Success! By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment. Remove test database and access to it? [Y/n]
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- Removing privileges on test database...
... Success! Reloading the privilege tables will ensure that all changes made so far
will take effect immediately. Reload privilege tables now? [Y/n]
... Success! All done! If you've completed all of the above steps, your MySQL
installation should now be secure. Thanks for using MySQL! Cleaning up...

建立 SQLAlchemy 和 Mysql 的连接

SQLAlchemy 通过一个特殊的 URI 字符串来创建与数据库的连接,一般的格式如下:

database_type+driver://user:password@sql_server_ip:port/database_name

我们需要在 config.py 文件中定义这一 URI:

class Config(object):
"""Base config class."""
pass class ProdConfig(Config):
"""Production config class."""
pass class DevConfig(Config):
"""Development config class."""
DEBUG = True
# MySQL connection
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:fanguiju@127.0.0.1:3306/jmilkfansblog'

最新文章

  1. C#异常处理的几个原则
  2. 4-printf & scanf函数
  3. 《Python核心编程》18.多线程编程(三)
  4. css整理-04 基本视觉格式化
  5. 解决ajax中文乱码问题
  6. CSS 实现:父元素包含子元素,子元素垂直居中布局
  7. JQuery中操作Css样式
  8. AIX-du
  9. Codeforces 351B Jeff and Furik
  10. Mockplus设计大赛获奖选手专访 | High音:轻松生活,随心嗨音
  11. [SDOI2005]反素数
  12. 百度前端学院-基础学院-第七天到第八天之BFC
  13. JavaWeb——jsp-config
  14. cinder 和 qt5 vs2015结合
  15. 写在vue总结之前(二)
  16. oracle10-11数据库下载
  17. [转]Hash碰撞冲突解决方法总结
  18. [Golang学习笔记] 04 程序实体1 变量声明
  19. Android上HDMI介绍(基于高通平台)
  20. confluence安装、破解+MariaDB驱动、汉化

热门文章

  1. 项目质量管理—七种基本质量工具
  2. CSS入门之盒模型(六分之四)
  3. 82、TensorFlow教你如何构造卷积层
  4. split函数实现
  5. windows 配置msys2环境
  6. Portal for ArcGIS启动失败,无法访问任何门户计算机,请联系您的系统管理员。
  7. Git 设置和取消代理(SOCKS5代理)
  8. 使用IDEA快速搭建Springboot项目
  9. Emacs 25.1 error solved: url-http-create-request: Multibyte text in HTTP request
  10. python基础【第五篇】