sqlite庫
BASE_DIR = os.path.dirname(os.path.abspath(file)
SQLALCHEMY_DATABASE_URI = ‘sqlite:////’+ os.path.join(BASE_DIR,‘develop.db’)
請求格式定製
parser = reqparse.RequestParser()
- 普通參數
parser.add_argument(‘username’, type=str, required=True, help=‘請提供用戶名’)
註冊接口
token:user.id
cache.set(user.token, user.id, timeout=60)
激活接口
userid = cache.get(token)
cache.delete(token)
加密方法
generate_password_hash(password): 輸入相同,但每次輸出結果都是不一樣的
check_password_hash(hash,password): 出入hash與輸入的值比較是否相等
上傳圖片
-
圖片格式
parser.add_argument(‘headimg’, type=werkzeug.datastructures.FileStorage, location=‘files’,required=True, help=‘請選擇圖片’) -
settings.py文件中
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
UPLOAD_FOLDER = os.path.join(BASE_DIR, ‘App/static/img/’) -
圖片數據
imgfile = parse.get(‘headimg’) -
圖片名稱
secure_filename(imgFile.filename)
filename = ‘%d-%s’ % (user.id,secure_filename(imgfile.filename)) -
圖片路徑
filepath = os.path.join(UPLOAD_FOLDER, filename) -
保存文件
imgfile.save(filepath)
JSON數據傳到數據庫
import json
import pymysql
數據庫連接
db = pymysql.Connect(host=‘127.0.0.1’,port=3306, user=‘root’, password=‘1234’, database=‘python1807tpp’, charset=‘utf8’)
遊標
cursor = db.cursor()
打開文件,讀取文件數據
with open(‘city.json’, ‘r’) as f:
# 加載json
city_collection = json.load(f)
# 數據源
returnValue = city_collection['returnValue']
# 獲取所有的key
letters = returnValue.keys()
# 遍歷操作
for letter in letters:
# 存入數據庫 letter
db.begin()
# INSERT INTO letter(name) VALUES(letter)
cursor.execute("INSERT INTO letter(name) VALUES('{}')".format(letter))
db.commit()
# 獲取字母對應的城市
cities = returnValue[letter]
# 獲取該字母對應的id 【插入城市數據時,需要指定是屬於哪個字母 id】
db.begin()
# SELECT * FROM letter WHERE name=letter
cursor.execute("SELECT * FROM letter WHERE name='{}'".format(letter))
db.commit()
# 取出字母對應的id
letter_result = cursor.fetchone()
letter_id = letter_result[0]
# 遍歷該字母對應的城市
for city in cities:
print(city)
db.begin()
# INSERT INTO city(id,cityCode, pinYin,regionName,c_letter) VALUES()
cursor.execute("INSERT INTO city(id,cityCode, pinYin,regionName,c_letter) VALUES('{}','{}','{}','{}','{}')".format(city['id'],city['cityCode'], city['pinYin'], city['regionName'], letter_id))
db.commit()
設置cookie操作
$.cookie(key, value, option)
$.cookie(key, {exprires:3, path:"/"})
獲取cookie
$.cookie(key)
刪除cookie
$.cookie(key, null)