def insert_data(): db.drop_all() db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_david = User(username='******', role=user_role) db.session.add_all( [admin_role, mod_role, user_role, user_john, user_susan, user_david]) db.session.commit()
def test_age(self): user = User("yangbo") db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.age, result.age)
def test_3(self): user = User("Huajiacheng", age = 24) db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.age, result.age)
def test2(self): user = User("lijiaqi", age=24) db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.age, result.age)
def test_1(self): user = User("Huajiacheng") db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.username, result.username)
def index(): form = NameForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.name.data).first() print(user) if user is None: user = User(username=form.name.data) db.session.add(user) session['known'] = False else: session['known'] = True session['name'] = form.name.data form.name.data = '' return redirect(url_for('index')) return render_template('index_3.html', form=form, name=session.get('name'), known=session.get('known', False))
def test_creatUser(self): a = User('shenqianqian') self.assertEqual(a.username, 'shenqianqian')
def test(self): user = User("lijiaqi") db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.username, result.username)
# !/usr/bin/env python # -*- coding: utf-8 -*- # Author : 湛允鹏 # Link : from hello import db from hello import User, Role db.drop_all() db.create_all() admin_role = Role(name='Admin') mod_roel = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_steve = User(username='******', role=user_role) db.session.add_all( [admin_role, mod_roel, user_role, user_john, user_susan, user_steve]) db.session.commit() print(admin_role.id) print(mod_roel.id) print(user_role.id) admin_role.name = 'Administrator' db.session.add(admin_role) db.session.commit() db.session.delete(mod_roel) db.session.commit()
# coding:utf-8 from hello import db from hello import Role, User db.create_all() db.drop_all() db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_dada = User(username='******', role=mod_role) print(admin_role.id) print(mod_role.id) print(user_role.id) db.session.add(admin_role) db.session.add(mod_role) db.session.add(user_role) db.session.add(user_john) db.session.add(user_dada) db.session.add(user_susan) db.session.commit() # 数据库会话 db.session 与 Flask session 对象没有关系。数据库会话也称为事务。 print(admin_role.id)
from hello import app from hello import db from hello import User, Role print app.url_map user_role = Role(name='user') user_gaoshine = User(username='******', role=user_role)
def test_userAge(self): b = User('shenqianqian') self.assertEqual(b.age, 20)
def test3(self): user = User("fwj3", age=45) db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.age, result.age)
def test_reset(self): user = User("zhouhao", age=21) db.session.add(user) db.session.commit() result = db.session.query(User).all()[0] self.assertEqual(user.age, result.age)
def test_three(self): user = User("wangyiping") user.age = 24 self.assertEqual(user.age, 24)
def test_two(self): user = User("wangyiping") self.assertEqual(user.age, 20)
def test_one(self): user = User("wangyiping") db.session.add(user) db.session.commit() test = db.session.query(User).all()[0] self.assertEqual(user.username, test.username)
from hello import db, Role, User db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_tony = User(username='******', role=user_role) db.session.add(Role(name='foobar')) db.session.add(admin_role) db.session.add(mod_role) db.session.add(user_role) db.session.add(user_john) db.session.add(user_susan) db.session.add(user_tony) db.session.commit()
def test_userAgeInput(self): c = User('shenqianqiann', 24) self.assertEqual(c.age, 24)
from hello import db """ 데이터베이스가 이미 존재할 경우 테이블을 새로 생성하거나 업데이트 하지 않기에 데이터베이스를 지우고 다시 만든다. """ db.drop_all() db.create_all() # database 생성 """행 추가""" from hello import Role, User admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') user_john = User(username='******', role=admin_role) user_susan = User(username='******', role=user_role) user_david = User(username='******', role=user_role) """ 오브젝트는 단지 파이썬에만 존재하며 아직 데이터베이스는 작성되지 않았다. id값이 아직 설정되지 않았기 때문 """ print(admin_role.id) print(mod_role.id) print(user_role.id) """ 데이터베이스에 대한 변경 사항은 데이터베이스 세션을 통해 관리됨. 데이터베이스 작성을 위해 오브젝트를 준비하기 위해서는 오브젝트가 세션에 추가되어야 한다.""" db.session.add(admin_role) db.session.add(mod_role) db.session.add(user_role) db.session.add(user_john) db.session.add(user_susan) db.session.add(user_david)