def init_database(): """ 初始化数据库 """ db.drop_all() db.create_all() for i in range(100): db.session.add( User( 'User' + str(i), 'a' + str(i), 'http://jira.internal.hyperchain.cn/secure/useravatar?avatarId=' + str(random.randint(10344, 10352)))) db.session.add( Image( 'http://08.imgmini.eastday.com/mobile/20171211/20171211181930_917f4f40b9fbaf2e06769b8fc77eefb2_1.jpeg', i + 1)) for j in range(5): db.session.add( Image( 'http://jira.internal.hyperchain.cn/secure/useravatar?avatarId=10352' + str(random.randint(10344, 10352)), i + 1)) for k in range(3): db.session.add( Comment('This is a comment {0}'.format(k + 1), i + 1, i + 1)) db.session.commit()
def init_database(): for i in range(0, 10): # 添加用户 db.session.add(User('user' + str(i), 'a' + str(i))) for j in range(0, 3): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 1 + 3 * i + j, j + 1)) db.session.commit() for i in range(5, 10): user = User.query.get(i) user.username = '******' + user.username db.session.commit() print(1, User.query.all()) print(2, User.query.get(4)) image = Image.query.get(26) image.url = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1268348955,3754655044&fm=26&gp=0.jpg' db.session.commit() for i in range(0, 10): image = Image(get_image_url(), 10) db.session.add(image) db.session.commit() xx_url = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1268348955,3754655044&fm=26&gp=0.jpg' xx_image = Image(xx_url, 11) db.session.add(xx_image) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'a' + str(i))) for j in range(0, 6): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 6 * i + j + 1, i + 1)) db.session.commit() ''' print 1,User.query.all() print 2,User.query.get(3) print 3,User.query.filter_by(id=5).all() print 4,User.query.filter(User.username.endswith('0')).limit(3).all() print 5,User.query.filter(and_(User.id>88,User.id<90)).all() print 6,User.query.paginate(page=1,per_page=11).items image = Image.query.get(1) print 8,image.user User.query.filter_by(id=2).update({'username':'******'}) db.session.commit() for i in range(50,100,2): comment = Comment.query.get(i+1) db.session.delete(comment) db.session.commit() ''' for i in range(0, 4): image = Image.query.get(600 - i) db.session.delete(image) db.session.commit()
def init_database_king(): urls = get_upload_url() print(urls) db.drop_all() db.create_all() with open('.\herolist.json', 'r', encoding='utf-8') as ff: jsonFile = json.load(ff) #print(jsonFile) n = 0 for i in range(0, len(jsonFile)): #英雄编号 eName = jsonFile[i]['ename'] # 英雄名字 cName = jsonFile[i]['cname'] title = jsonFile[i]['title'] skinName = jsonFile[i]['skin_name'] skinName = skinName.split("|") # 皮肤数量 skin_numbers = len(skinName) print(str(eName) + cName + title + str(skin_numbers)) db.session.add(User(cName, 'herorobort')) #账号密码 for j in range(1, skin_numbers + 1): pictureUrl = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str( eName) + '/' + str(eName) + '-bigskin-' + str(j) + '.jpg' db.session.add(Image(pictureUrl, i + 1)) n = n + 1 db.session.add(Comment(skinName[j - 1], n, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('newke' + str(i), 'a' + str(i))) for j in range(0, 3): #每人发三张图 db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('comment' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() for i in range(60, 100, 10): # 通过update函数 User.query.filter_by(id=i + 1).update({'username': '******' + str(i)}) db.session.commit() for m in range(5, 50, 2): # 通过设置属性 newname = User.query.get(m) newname.username = '******' + newname.username db.session.commit() User.query.filter_by(id=11).update({'username': '******'}) db.session.commit() for i in range(51, 100, 2): comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit()
def init_database(): db.drop_all() # 删除所有的表 db.create_all() # 根据user中的代码创建表(根据列) for i in range(0,100): # 创建用户(用户名,密码) db.session.add(User('User'+str(i),'a'+str(i))) # 给每个人加几张图片,db.session.add(class)——创建类 for j in range(0,10): db.session.add(Image(get_image_url(),i+1)) for k in range(0,3): db.session.add(Comment('This is comment '+str(k),1+10*i+j,i+1)) db.session.commit() #数据库事物的提交,没提交没用! #更新数据 #直接取.username更新;update+字典形式更新 for i in range(50,100,2): user = User.query.get(i) user.username = '******'+user.username for i in range(51,100,2): User.query.filter(User.id==i).update({'username':'******'+str(i)}) db.session.commit() #删除评论 for i in range(50,100,2): comment = Comment.query.get(i) db.session.delete(comment) db.session.commit()
def init_database2(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('user' + str(i + 1), 'a' + str(i + 1))) for j in range(0, 7): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add(Comment('hello' + str(k), 1 + 7 * i + j, i + 1)) db.session.commit() for i in range(1, 16): user = User.query.get(i) user.username = '******' + user.username db.session.commit() for i in range(20, 30): # User.query.filter_by(id=i).update({'username':'******'+str(i)}) User.query.filter_by(id=i + 1).update({'username': '******' + str(i)}) db.session.commit() # 两种删除 for j in range(20, 30): comment = Comment.query.get(j) db.session.delete(comment) # print(1,comment) db.session.commit() for j in range(40, 50): Comment.query.filter_by(id=j).delete() db.session.commit()
def init_database(): db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i), 'a' + str(i))) for j in range(0, 5): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 5): db.session.add(Comment('这是一条评论' + str(k), 1 + 5 * i + j, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('user' + str(i), str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add(Comment('小明给你评论了哦 ' + str(k), 1 + 10 * i + j, 1 + i)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0,100): db.session.add(User('User'+str(i+1),'a'+str(i))) for j in range(0,3): db.session.add(Image(get_image_url(),i+1)) for k in range(0,3): db.session.add(Comment('This is a commnet'+str(k),1+3*i+j,i+1)) db.session.commit()
def upload(): file = request.files['file'] if file.filename.find('.') > 0: file_ext = file.filename.rsplit('.', 1)[1].strip().lower() if file_ext in app.config['ALLOWED_EXT']: file_name = str(uuid.uuidl()).replace('-','')+'.'+file_ext url = save_to_local(file,file_name) if url != None: db.session.add(Image(url, current_user.id)) db.session.commit() return redirect('/profile/%d'%current_user.id)
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('newcode' + str(i), 'a' + str(i))) for j in range(0, 10): #每人发十张图 db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('newcode' + str(k), 1 + 10 * i + j, i + 1)) db.session.commit()
def upload(): file = request.files['file'] # 保存上传文件 if file.filename.find('.') > 0: # 鉴定文件名是否符合要求 file_ext = file.filename.rsplit('.', 1)[1].strip().lower() if file_ext in app.config['ALLOWED_EXT']: file_name = str(uuid.uuid1()).replace('-', '') + '.' + file_ext # 随机值 url = save_to_local(file, file_name) # url = qiniu_upload_file(file, file_name) if url != None: db.session.add(Image(url, 102)) db.session.commit() return redirect('/profile/%d' % current_user.id) '''
def init_database(): #数据库清空 db.drop_all() #数据库创建 db.create_all() #数据库添加数据:用户名和密码(这个顺序见nowstagram.models里面User类的初始化顺序) for i in range(0, 100): db.session.add(User('User' + str(i), 'a' + str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment ' + str(k), 1 + 10 * i + j, i + 1)) #提交事务,如果不提交的话等于没做 db.session.commit() #更新User表的username属性 for i in range(50, 100, 2): user = User.query.get(i) user.username = '******' + user.username #for i in range(51, 100, 2): #User.query.filter_by(id=i).update({'username':'******'}) User.query.filter_by(id=51).update({'username': '******'}) db.session.commit() #删除评论 for i in range(50, 100, 2): comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit() #查询数据 print 1, User.query.all() print 2, User.query.get(3) print 3, User.query.filter_by(id=5).first() print 4, User.query.order_by(User.id.desc()).offset(1).limit(2).all() print 32, User.query.filter_by(id=5) print 42, User.query.order_by('id desc').limit(2) print 5, User.query.filter(User.username.endswith('0')).limit(3).all() print 6, User.query.filter(or_(User.id == 88, User.id == 99)).all() print 7, User.query.filter(and_(User.id > 88, User.id < 93)).all() print 8, User.query.filter(and_( User.id > 88, User.id < 93)).first_or_404() #有的话就输出第一个没有的话就输出404 print 9, User.query.paginate(page=1, per_page=10).items print 9, User.query.order_by(User.id.desc()).paginate(page=1, per_page=10).items user = User.query.get(1) print 10, user.images image = Image.query.get(1) print 11, image.user
def upload(): print 'request.files', request.files file = request.files['file'] print dir(file) print 'file.name', file.name file_name = str(uuid.uuid1()) + '.' + 'jpg' url = save_to_loacal(file, file_name) print "url:", url if url: db.session.add(Image(url, current_user.id)) db.session.commit() return redirect('/profile/{}'.format(current_user.id))
def init_database(): # 有100个用户,每个用户三张图片,每个图片三个评论 db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'a' + str(i))) # 对user进行写入 for j in range(0, 3): db.session.add(Image(get_image_url(), i + 1)) # 对图片数据库进行写入 for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() # 提交往表里写数据的命令一定记得是在第一个for循环里!!!!!!!要不然没法写入 # 数据库更新 for i in range(50, 100, 2): # 50到100的偶数的用户名进行修改 user = User.query.get(i) user.username = '******' + user.username User.query.filter_by(id=51).update({'username': '******'}) # 第二种更新 db.session.commit() # 数据库中删除一些评论 for i in range(50, 100, 2): # 编号50到100的双数编号评论删除 comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit() # 数据库数据查询 print(1, User.query.all()) # 查询所有 print(2, User.query.get(3)) # 查询指定数据 print(3, User.query.filter_by(id=5).first()) # 查询排第五的 print(4, User.query.order_by(User.id.desc()).offset(1).limit( 2).all()) # id的降序,排完序列后offset偏移一个,在打出两个limit print(5, User.query.filter(User.username.endswith('0')).limit( 3).all()) # 打出结尾是0的,但limit3个,只打出来三个 print(6, User.query.filter(or_(User.id == 88, User.id == 99)).all()) print(7, User.query.filter(and_(User.id > 88, User.id < 93)).all()) print(8, User.query.filter(and_(User.id > 88, User.id < 93)).first_or_404()) print(9, User.query.order_by(User.id.desc()).paginate( page=1, per_page=10).items) # 给数据库数据逆序分页,一页10个人 # 一对多查询 user = User.query.get(1) print(10, user.images) # 把user1用户的图片全部查询出来,因为有关联,和外键关联 image = Image.query.get(1) print(11, image, image.user) # 从图片来查找用户,在models里面要进行一下关联
def init_database(): db.drop_all() db.create_all() get_image_url() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'a' + str(i + 1))) for j in range(0, 10): db.session.add(Image(imgs_url[random.randint(0, j * 200)], i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 1 + 10 * i + j, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), '0000')) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 5): db.session.add( Comment('This is a comment' + str(k), 1 + 10 * i + j, i + 1)) db.session.commit() print "database initialized"
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i), 'a' + str(i))) for j in range(0, 3): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('this is comment ' + str(k), int(1 + 3 * i + j), int(i + 1))) db.session.commit() print 1, User.query.all() print 2, User.query.get(3)
def upload(): file = request.files['file'] # http://werkzeug.pocoo.org/docs/0.10/datastructures/ # 需要对文件进行裁剪等操作 file_ext = '' if file.filename.find('.') > 0: file_ext = file.filename.rsplit('.', 1)[1].strip().lower() if file_ext in app.config['ALLOWED_EXT']: file_name = str(uuid.uuid1()).replace('-', '') + '.' + file_ext url = qiniu_upload_file(file, file_name) #url = save_to_local(file, file_name) if url != None: db.session.add(Image(url, current_user.id)) db.session.commit() return redirect('/profile/%d' % current_user.id)
def init_database1(): urls = get_upload_url() un = 0 db.drop_all() db.create_all() for i in range(0, 3): db.session.add(User('user' + str(i + 1), 'a' + str(i + 1))) for j in range(0, 3): if un < len(urls): db.session.add(Image(get_image_url(), i + 1)) un = un + 1 for k in range(0, 5): db.session.add( Comment('hello' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'a' + str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is comment' + str(k), 1 + i * 10 + j, i + 1)) db.session.commit() for i in range(50, 100, 2): user = User.query.get(i) user.username = '******' + user.username # for i in range(51,100, 2): User.query.filter_by(id=51).update({'username': '******'}) db.session.commit() for i in range(50, 100, 2): commit = Comment.query.get(i + 1) db.session.delete(commit) db.session.commit() user = User.query.get(2) print(10, user.images) # test query print(1, User.query.all()) print(2, User.query.get(3)) print(3, User.query.filter_by(id=5).first()) print(4, User.query.order_by(User.id.desc()).offset(1).limit(2).all()) print(5, User.query.filter(User.username.endswith('0')).limit(3).all()) print(6, User.query.filter(or_(User.id == 88, User.id == 99)).all()) print(7, User.query.filter(or_(User.id >= 88, User.id <= 99)).all()) print(8, User.query.filter(or_(User.id >= 100, User.id <= 0)).first_or_404()) print( 9, User.query.order_by(User.id.desc()).paginate(page=1, per_page=10).items) user = User.query.get(1) print(10, user.images, get_image_url()) image = Image.query.get(2) print(11, get_image_url(), image.user)
def init_database(): db.drop_all() db.create_all() for i in range(0,100): db.session.add(User('User'+str(i+1),'a'+str(i+1))) for j in range(0,3): db.session.add(Image(get_image_url(),i+1,)) for k in range(0,3): db.session.add(Comment('This is a comment'+str(k),1+3*i+j,i+1)) db.session.commit() for i in range(50,100,2): user=User.query.get(i) user.username='******'+user.username User.query.filter_by(id=51).update({'username':'******'}) db.session.commit() for i in range(50,100,2): comment=Comment.query.get(i+1) db.session.delete(comment) db.session.commit() print 1,User.query.all() print 2,User.query.get(3) print 3,User.query.filter_by(id=5).first() print 4,User.query.order_by(User.id.desc()).offset(1).limit(2).all() print 5,User.query.filter(User.username.endswith('0')).limit(3).all() print 6,User.query.filter(or_(User.id==88,User.id==99)).all() print 7,User.query.filter(and_(User.id>88,User.id<92)).all() print 8,User.query.filter(and_(User.id>88,User.id<92)).first_or_404() print 9,User.query.order_by(User.id.desc()).paginate(page=1,per_page=10).items user=User.query.get(1) print 10,user.images image=Image.query.get(1) print 11,image.user
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i), 'a' + str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k + 1), 1 + 10 * i + j, i + 1)) db.session.commit() print 1, User.query.all() print 2, User.query.get(3) user = User.query.get(1) print user.images image = Image.query.get(1) print image.user
def init_database(): db.drop_all() db.create_all() # 100个用户,每个用户3张图片,每张图片3条评论 for user_i_id in range(0, 30): db.session.add( User(u'匿名用户' + str(user_i_id + 1), 'admin' + unicode(user_i_id) + '@qq.com', 'password' + str(random.randint(0, 1000)))) # id从1开始 for image_j_id in range(0, 7): # 看Image类中的构造函数__init__的参数是几个,这里就传入几个,相当于类的实例化(或者说是表中一条数据的建立) # 第2个参数就是用的user_id,故从上面的遍历中直接传下来 db.session.add(Image(get_image_url(), user_i_id + 1)) for comment_k_id in range(0, 3): db.session.add( Comment(u'这是一条评论:你长得真好看' + str(comment_k_id), 1 + 7 * user_i_id + image_j_id, user_i_id + 1)) # 后面两个是image_id user_id,需要计算一下才是对应当前评论id的正确值; db.session.commit() # 将数据插入后,需要commit一下,才能将数据真正的提交的数据库中; '''
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'a' + str(i))) for j in range(0, 3): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() for i in range(50, 100, 2): user = User.query.get(i) user.username = '******' + user.username #User.query.filter_by(id=51).update({'username':'******'+user.username}) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('牛客' + str(i), 'a' + str(i))) for j in range(0, 3): #每人发三张图 db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add(Comment('这是一条评论' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() # 更新 for i in range(0, 100, 10): # 通过update函数 User.query.filter_by(id=i + 1).update({'username': '******' + str(i)}) for i in range(1, 100, 2): # 通过设置属性 u = User.query.get(i + 1) u.username = '******' + str(i * i) db.session.commit() # 删除 for i in range(50, 100, 2): Comment.query.filter_by(id=i + 1).delete() for i in range(51, 100, 2): comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit() print 1, User.query.all() print 2, User.query.get(3) # primary key = 3 print 3, User.query.filter_by(id=2).first() print 4, User.query.order_by(User.id.desc()).offset(1).limit(2).all() print 5, User.query.paginate(page=1, per_page=10).items u = User.query.get(1) print 6, u print 7, u.images print 8, Image.query.get(1).user
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('牛客' + str(i), 'a' + str(i))) for j in range(0, 10): # 每人发十张图 db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add(Comment('这是一条评论' + str(k), 1 + 10 * i + j, i + 1)) db.session.commit() # ''' # 更新 for i in range(0, 100, 3): # 通过update函数 User.query.filter_by(id=i).update({'username': '******' + str(i)}) # User.query.filter(User.username.endswith('0')).update({'username': '******' + User.username}, synchronize_session=False) # filter_by多个参数是and User.query.filter_by(id=4, password='******').update({'username': '******' + str(i)}) User.query.filter_by(id=5, password='******').update({'username': '******' + str(i)}) for i in range(7, 100, 10): # 通过设置属性 u = User.query.get(i) u.username = '******' + str(i * i) db.session.commit() # 删除 for i in range(50, 100, 2): Comment.query.filter_by(id=i + 1).delete() for i in range(51, 100, 2): comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit() print(User.query.all())