flask 增刪改查

2022-06-30 01:12:09 字數 742 閱讀 4569

使用flask-sqlachemy建立模型與表的對映

模型需要繼承自db.model ,然後需要對映到表中的屬性,必須寫成db.column的資料型別

資料型別db.integer代表整形,db.string是varchar,db.text

是文字型別。

其他引數

呼叫db.create_all()來將模型建立到資料庫中

使用flask-sqlachemy:增刪改查

增:artical = artical(title='aaa',content='bbb')

db.session.add(artical)

# 事務

db.session.commit()

查:result = artical.query.filter(artical.title=='aaa').first()

result = result[0]

print(result.title)

改result = artical.query.filter(artical.title=='aaa').first()

result.title = 'new title'

db.session.commit()

刪除result = artical.query.filter(artical.content=='bbb').first()

db.session.delete(result)

db.commit()

Flask 增刪改查

增 語法 例項化類author傳遞以字段為引數的資料進去 類名 欄位名1 資料1 欄位名2 資料2 new author1 author name 小明 id 1new author2 author name 小華 id 2id自動生成的,不需要傳遞 有外來鍵的表的增加 new book1 book...

flask框架中增刪改查

資料庫操作 新增資料 根據資料建立模型物件 db.session.add 物件 db.session.commit 更新根據條件找到對應的物件 直接修改物件的屬性值即可 db.session.commit 刪除物理刪除 根據條件找到對應的物件 db.session.delete 物件 db.sess...

mysql增刪改查效果 mysql增刪改查

檢視所有資料庫 mysql show databases 建立乙個庫ghd並指定字符集為utp8 mysql create database ghd charset utf8 檢視mysql支援的字符集 mysql show char set 建立乙個表,並設定id為主鍵 create table ...