Day6-python Study Notes (13) Redis Database

Source: Internet
Author: User
Tags sessions

Import Redis
r = Redis. Redis (host= ' 211.149.218.16 ', port=6379,password= ' 123456 ', db=2) #连上redis
Print (R.get (' hahahsfdfsdf '))
#r. Set (' Nhy_session ', ' 201801211505 ') #set数据
# Print (R.get (' Mapepe '). Decode ()) #redis里面取出来的数据都是bytes类型的, so use the. Decode method to convert to a string
# r.delete (' Mapepe ') #删除一个
#r. Setex (' Nhy ', ' hahah ', ') #可以指定key的失效时间, Unit is seconds '

# Set Get delete Setex are all for string type k-v

#hash类型
# R.hset (' Sessions ', ' nhy ', ' 123456 ') #插入数据
# R.hset (' Sessions ', ' YBQ ', ' 1234562 ')
# R.hset (' Sessions ', ' xsr ', ' 1234561 ')
#print (R.hget (' Sessions ', ' XSR ')) #获取数据
# redis_data = R.hgetall (' sessions ') #获取到hash类型里面所有的数据
# all_data = {}
# for K,v in Redis_data.items (): #把hash类型里面所有的数据转成正常的字典
# k = K.decode ()
# v = v.decode ()
# All_data[k]=v
#hash类型没有过期时间

#下面这种是有层级的
# r.set (' TXZ:YBQ ', ' Not Delivered ') #
# r.set (' Txz:haixia ', ' handed in ') #
# Print (R.keys ()) #获取所有的key
# Print (R.keys (' txz* ')) #以txz开头的key
# Print (R.type (' Sessions ')) #获取key的类型

# get something from a database in Redis to another database.
# 1, establish two Redis connections
#1, src
# 2, Target
#2, get to All Key,kyes ()
# 3, determine the type of key, string hash

def Op_redis (host,password,k,v=none,port=6379,db=0):
r = Redis. Redis (HOST=HOST,PASSWORD=PASSWORD,PORT=PORT,DB=DB)
If V:
R.set (k,v)
res = ' OK '
Else:
res = r.get (k)
If res: #这里是判断有没有get到数据
res = Res.decode ()
Else:
res = None
return res

# Print (__name__)
# print (' Haha, I'm here in the head ')
# if __name__== ' __main__ ':
# #别人导入这个python文件的时候, the following code will not be executed
# #自己测试的时候用
# Print (__name__) # __main__
# # print (' haha haha haha, whether or not to execute ')
# sql = ' select * ' from Bt_stu limit 5; '
# sql2 = ' Update bt_stu set class= ' Scorpio 3 ' where id=503; '
# res = Op_mysql (
# host= ' 211.149.218.16 ',
# user= ' jxz ', password= ' 123456 ', #port这里一定要写int类型
# port=3306,db= ' jxz ', charset= ' UTF8 ', sql=sql2)
# Print (res)

Redis Data Migration
Import Redis

Src_redis = Redis. Redis (host= ' 211.149.218.16 ', port=6379,password= ' 123456 ', db=2) #连上redis
Target_redis = Redis. Redis (host= ' 211.149.218.16 ', port=6379,password= ' 123456 ', db=14) #连上redis
For key in Src_redis.keys ():
if Src_redis.type (key) = = B ' String ': #判断key的类型 because the Redis data is binary, so this is also used bytes
v = src_redis.get (key) #先获取到原来的数据
Target_redis.set (key,v) #再set到新的里面
Else:
all_hash_data = Src_redis.hgetall (key) #先获取到hash类型里面所有的数据
for K,v in All_hash_data.items (): #因为hash类型的获取到之后是一个字典, so here's The Loop dictionary
Target_redis.hset (key,k,v) #key是外面的大key, K is inside the small k,v is small k corresponding value




Day6-python Study Notes (13) Redis Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.