Flask學習之旅--數據庫

1、寫在前面

  在Web開發中,數據庫操做是很重要的一部分,由於網站的不少重要信息都保存在數據庫之中。而Flask在默認狀況下是沒有數據庫、表單驗證等功能的,可是能夠用Flask-extension爲Web應用添加這些功能。html

 

2、Flask SQLite

  SQLite是一款輕型的數據庫,是遵照ACID的關係型數據庫管理系統。因爲Python對SQlite有內置的支持,所以在Flask應用程序中和SQLite進行交互是比較容易的。python

  首先須要建立一個SQLite數據庫「user.db」,並在其中建立一張用戶表。代碼以下:mysql

1 import sqlite3 2 
3 
4 conn = sqlite3.connect("user.db") 5 print("Connected!") 6 
7 conn.execute("CREATE TABLE USER(username TEXT, password TEXT, EMAIL TEXT)") 8 print("Table created successfully!") 9 conn.close()

  這裏就不貼HTML代碼了,就是一個註冊頁面,在註冊的時候會將用戶輸入的用戶名、密碼和郵箱傳到後臺。在app.py中須要導入sqlite3模塊,而後鏈接前面建立的user.db,在創建鏈接以後建立一個遊標對象,而後編寫SQL語句進行數據庫操做,整個過程都算是比較容易的。app.py中的代碼以下:sql

 1 from flask import Flask, render_template, request  2 import sqlite3 as sql  3 
 4 app = Flask(__name__)  5 
 6 
 7 @app.route('/register', methods=['GET', 'POST'])  8 def register():  9     if request.method == 'GET': 10         return render_template('register.html') 11     else: 12         msg = ""
13         try: 14             username = request.form["usr"] 15             password = request.form["pwd"] 16             email = request.form["email"] 17             print(username, password, email) 18             with sql.connect("user.db") as con: 19                 cur = con.cursor() 20                 cur.execute("INSERT INTO USER (username, password, email) VALUES (?,?,?)", 21  (username, password, email)) 22  con.commit() 23             msg = "註冊成功!"
24         except: 25  con.rollback() 26             msg = "註冊失敗!請重試!"
27         finally: 28  con.close() 29             return render_template('register.html', msg=msg) 30 
31 
32 if __name__ == '__main__': 33     app.run()

 

3、Flask SQLAlchemy

  Flask-SQLAlchemy是Flask擴展,它將對SQLAlchemy的支持添加到Flask應用程序中。在Flask Web應用程序中使用原始SQL對數據庫執行CRUD操做可能很繁瑣,不過SQLAlchemy 爲應用程序開發人員提供了SQL的所有功能和靈活性。它是一個對數據庫的抽象,讓開發者不用這些編寫SQL語句,而是使用其提供的接口去操做數據庫,這其中涉及到一個很是重要的思想:ORM(Object Relation Mapping,對象關係映射),主要的功能是實現模型對象到關係型數據庫數據的映射。說白了就是使用經過對象去操做數據庫。數據庫

1.Flask-SQLAlchemy安裝

  使用pip install flask-sqlalchemy進行安裝。不過在安裝的時候可能會出現以下錯誤:flask

pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.網絡

  這是由於在下載python庫的時候,因爲國內網絡緣由,致使python包的下載速度很是慢,查看pip 文檔,只要在 pip的時候控制超時便可, 具體參數爲 --default-timeout=100, 後面的時間能夠本身指定。所以能夠用以下命令進行下載安裝:session

pip install --default-timeout=100 flask-sqlalchemyapp

2.Flask-SQLAlchemy配置

  今後模塊導入SQLAlchemy類,建立一個Flask應用程序對象併爲要使用的數據庫設置URI的代碼以下:測試

from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)

# URI的格式爲:用戶名:密碼@ip地址:端口號(默承認以不寫)/數據庫名
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:qwer1234@localhost/flask"

3.配置的時候可能出現的問題及解決辦法

1)ModuleNotFoundError: No module named 'MySQLdb'

解決辦法:

  使用pymysql模塊,URI改成:

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:qwer1234@localhost/flask"

2)Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 481")

解決辦法:

  改用mysql-connector,使用pip install mysql-connector下載安裝,URI改成:

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://root:qwer1234@localhost/flask"

3)sqlalchemy.exc.NotSupportedError: (mysql.connector.errors.NotSupportedError) Authentication plugin 'caching_sha2_password' is not supported

解決辦法:

  出現這個錯誤是由於MySQL8.0採用了Use Strong Password Encryption for Authentication即強密碼加密,而mysql.connector的引擎不支持caching_sha2_password的加密格式,因此解決思路有以下幾種:

  (1)重裝MySQL,在Authentication Method中選擇第二項(重裝比較麻煩,我就沒有嘗試):

  (2)在client端,將加密格式選擇成mysql_native_password,命令以下(我的嘗試後無效):

ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'

  (3)最後一種是我本身摸索出來的,就是在URI設置的時候加上一個參數auth_plugin並設置爲mysql_native_password:

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://root:qwer1234@localhost/flask?auth_plugin=mysql_native_password"

4.Flask-SQLAlchemy的基本操做

1) 經常使用查詢過濾器:

  過濾器獲得的還只是一些對象,須要使用執行器來獲取真正的數據。

filter(): 把過濾器添加到原查詢上,返回一個新查詢,須要使用模型類名去獲取字段來進行比較;

filter_by():把等值(只能使用=比較操做)過濾器添加到查詢上,返回一個新查詢;

order_by():根據指定條件對查詢結果進行排序,返回一個新查詢;

group_by():根據指定條件對原查詢結果進行分組,返回一個新查詢。

2.)經常使用查詢執行器

all():以列表的形式返回查詢的全部結果;

first():返回查詢的第一個結果;

first_or_404():同first(), 只不過若是沒有找到的話,返回404錯誤;

get():返回指定主鍵對應的行;

get_or_404():返回指定主鍵對應的行,如不存在,返回404錯誤;

count():返回查詢結果的數量;

paginate():返回一個Paginate對象,包含指定範圍內的結果。

3.)查詢條件

startswith('xx'):查詢以xx開頭的全部數據;

endswith('xx'):查詢以xx結尾的全部數據;

not_():取反;

and_():返回and()條件知足的全部數據;

or_():返回or()條件知足的全部數據。

5.使用示例

  app.py中代碼以下:

 1 from flask_sqlalchemy import SQLAlchemy
 2 import mysql.connector
 3 
 4 
 5 app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://root:qwer1234@localhost/flask?auth_plugin=mysql_native_password"
 6 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
 7 app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
 8 
 9 
10 # 獲取SQLAlchemy實例對象
11 db = SQLAlchemy(app)

  而後新建一個model.py,編寫以下代碼進行測試:

 1 from app import db
 2 
 3 
 4 # 建立模型對象
 5 class User(db.Model):
 6     __tablename__ = "users"
 7     id = db.Column(db.Integer, primary_key=True)
 8     username = db.Column(db.String(16), unique=True)
 9     password = db.Column(db.String(16))
10     email = db.Column(db.String(32), unique=True)
11 
12     def __repr__(self):
13         return '<User %r>' % self.username
14 
15 
16 # 1.建立表
17 db.create_all()
18 print("Created Successfully!")
19 
20 # 2.增長記錄
21 usr1 = User()
22 usr1.id = 1
23 usr1.username = "wang"
24 usr1.password = "wangwang"
25 usr1.email = "wang@163.com"
26 usr2 = User(id=2, username="yang", password="yang", email="yang@163.com")
27 db.session.add(usr1)
28 print("Add usr1")
29 db.session.add(usr2)
30 print("Add usr2")
31 db.session.commit()
32 
33 # 3.查詢記錄,注意查詢返回對象,若是查詢不到返回None
34 users1 = User.query.all()  # 查詢全部
35 print(users1)
36 print("User Count:", len(users1))
37 
38 # 4.刪除
39 user = User.query.get(1)
40 db.session.delete(user)
41 print("Delete usr1")
42 db.session.commit()
43 
44 users2 = User.query.all()  # 查詢全部
45 print(users2)
46 print("User Count:", len(users2))

  運行結果以下所示:

Created Successfully!Add usr1Add usr2[<User 'wang'>, <User 'yang'>]User Count: 2Delete usr1[<User 'yang'>]User Count: 1

相關文章
相關標籤/搜索