SQLAlchemy 一些基本操作
建表:db.create_all() 一次性創建全部的表
插入數據:
? 1、創建變量user = User(username = “hjj2”,password = “1234”)
? 2、使用db.session.add(user),添加到會話對象中
? 3、使用db.session.commit(),提交到數據庫。
查詢數據:
? 1、查詢同類全部數據:users = User.query.all()
? 2、通過主鍵獲取,如:user = User.query.get(1)
? 3、有過濾的查詢,采用filter_by來過濾,如:peter = User.query.filter_by(username=’peter’).first()
? 4、復雜表達式查詢,如: User.query.filter(User.email.endswith(‘@example.com’)).all()
刪除數據:
? 1、db.session.delete(user)
? 2、db.session.commit()
更新數據:
? 1、在使用first()或者all()等方法返回數據之前,調用update方法可以修改已存在的數據的值。如:User.query.filter_by(username=’fake_name’).update({‘password’:’test’})
查詢數據:
問題循環引用的解決方案:http://blog.csdn.net/handsomekang/article/details/19010407
?