Python (12) Python operation Redis

Source: Internet
Author: User
Tags install redis

1, Python to operate Redis first need to install the Redis module, and then import to use

Installation: Pip Install Redis

Importing: Import Redis

2. Connect to Redis

R = Redis. Redis (host= ' 192.168.21.129 ', password= ' 123456 ', db=1,port=6379)

Password in configuration file redis.conf, port is generally 6379,db can be selected based on the database you need to operate

3, Redis additions and deletions to search

There are several types of values stored by key in Redis, which mainly describe string types and hash types. Use the command redis 127.0. 0.1:6379> type key_name to view the storage type of the value.  

The following is an operation R for a string type.Set('User3','e10adc3949ba59abbe56e057f20f883e') #数据库里面新增一个值, the modification is also Setr.setex ('User1','e10adc3949ba59abbe56e057f20f883e', -) #数据库里新增一个值 and set the expiration time of the key, and the last parameter is the second R.Set('Session:user1',{"Seessionid":"a5dfcb047721e02a6f8bff779c856165","Login_time":"201805081820"}) #新增值, the values are stored in a session folder with print (R.Get('User1') ) #获取对应key的值, the returned type is of type Byte, and the print (R) needs to be decoded.Get('User1'). Decode ()) #获取到的value值解码print (R.Get('Session:user1'). Decode ()) print (R.keys ()) #获取所有的key, there is a list inside, the element is a byte type, such as: [B'User3'B'User1'B'Session:user1'] #获取所有的key, and decode the output list= [K.decode () forKinchR.keys ()]print (list) #输出如: ['User3','User1','Session:user1']r.delete ('User3') #删除指定的key # Delete all keys need to loop forKinchR.keys (): R.delete (k)
The following is a hash-type hashing operation, nested dictionary r.hset ('Info','Zhang San','Beijing, undergraduate, test engineer') #hash类型存储方式会有两个key, big key (info) and small key (Zhang San), but cannot set the expiration time of the small key R.hset ('Info','John Doe','Hubei, PhD, Boss') R.hset ('Info','Harry','Jiangxi, PhD, CEO') R.hset ('LOGIN_MLL','test1','{"Seessionid": a5dfcb047721e02a6f8bff779c815165, "Login_time": 201805051820}') Print (R.hget ('Info','Zhang San'). Decode ()) #指定大key和小key获取对应的数据print (R.hgetall ('Info')) #获取里面所有的k和-V, all the k,v are stored in the dictionary, but are byte type # get inside all the K and-V, all the k,v are stored in the dictionary and decode the output info= R.hgetall ('Info') New_info= {} forKvinchInfo.items (): New_info[k.decode ()]=V.decode () print (New_info) R.hdel ('Info','Zhang San') #删除指定keyr. Delete ('Info') #删除整个keyr. Expire ('Info', -) #第一个key设置失效时间, unable to set the expiration time of the small key print (R.ttl ('Info')) #获取key的失效时间print (R.type ('Info')) #查看key是什么类型的

Small exercise: Import data from a table in a database into Redis

Analysis: 1, even the database, to find all the data in the database, cursor type to use Pymysql.curosrs.DictCour

2. Find all data [{"id": 1, "passwd": "49487dd4f94008a6110275e48ad09448", "username": "Niuhayang", "Is_admin": 1}]
3, loop This list, take to Usernamer, username as key
4. Turn this dictionary into JSON, and save it in OK.

Import Pymysql,json,redisr= Redis. Redis (host='118.24.3.40', password='hk139bc&*', db=1, port=6379) Conn= Pymysql.connect (host='118.24.3.40', user='Jxz', passwd='123456', db='Jxz', charset='UTF8') cur= Conn.cursor (cursor=pymysql.cursors.DictCursor) Cur.execute ('select * from My_user;') All_data=Cur.fetchall () #数据库某表里面所有数据, the output is in list form, each element in the list is each row of data in the table, and each row of data is in the form of a dictionary # such as: [{'username':'Niuniu','Is_admin':0,'ID':0,'passwd':'Niuniu'}, {'username':'xiaohei1234','Is_admin':0,'ID':0,'passwd':'aA123456'}, {'username':'Gyx','Is_admin':0,'ID':0,'passwd':'123456'}
forDatainchAll_data: #data为表中每行数据, is the dictionary K= data.Get('username') #将username的值获取, as a small key r.hset ('STU_INFO_MKK', K,json.dumps (data) #将data字典转为json字符串, added to Redis cur.close () conn.close ()

Python (12) Python operation Redis

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.