Python Foundation six-operational database

Source: Internet
Author: User
Tags redis

Operational database module: MySQL pymysql and Redis Redis, reference http://www.nnzhp.cn/blog/archives/402

1. Operation MySQL

ImportPymysqlconn=pymysql.connect (host='192.168.160.3', user='Root', port=3306,passwd='123456', db='hqtest', charset='UTF8')#Establish a database connection #关键字传参Couser=conn.cursor ()#create a cursor on the connectionSql='INSERT INTO HQ (Username,password) value ("HQ", "25906028D04BA0563447F41DCCB3C4CF")'Couser.execute (SQL2) conn.commit ()#Commit, INSERT, UPDATE, delete must commit to take effectSQL2='SELECT * FROM HQ'couser.execute (SQL)Print(Couser.fetchall ())#Get all dataCouser.scroll (1,mode='Absolute')#move the cursor, absolutePrint(Couser.fetchone ())#fetch one column of data per executionCouser.scroll (0,mode='relative')#move the cursor, relative to thePrint(Couser.fetchone ())#fetch one column of data per executionCouser=conn.cursor (Cursor=pymysql.cursors.dictcursor)#Specifies the dictionary type cursorsql3='SELECT * FROM HQ'Couser.execute (SQL3)Print(Couser.fetchall ()) Couser.close ()#Close CursorsConn.close ()#Close Connection

2. Operation of Redis

ImportRedis,jsonr=redis. Redis (host='192.168.160.3', port=6379,db=0,password='123456')#Redis has all the strings in it, and the data obtained from Redis is bytes type.#A String type keyR.set ('name', [1,2,3,4]) name= R.get ('name')Print(Name.decode ())#you want to use the. Decode method to turn it into a string to continue the operationNew_name = Json.loads (Name.decode ())#List and dictionaryPrint(Type (new_name)) R.setex ('Eee','hahaha', 15)#can set the expiration time of keyPrint(R.get ('HAHAHAAHAHAHASDFSDFSD'))#get non-existent key, that is, return noneR.mset (nhy='Hahahaha', eee_age=19999)#batch add, displacement set value when usedR.delete ('Eee')#Delete a keyPrint(R.keys ('*n*'))#get all the keys#Key of the hash typeR.hset ('user_session','Eee','Sdfjksdjflksfsdfsdfsfs') R.hset ('user_session','ooo','Sdfjksdjflksfsdfsdfsfs')Print(R.hget ('user_session','Eee'))#gets the value of K within the specified namePrint(R.hgetall ('user_session'))#get all the values inside the hash typeR.hdel ('user_session','Eee')#deletes the specified keyR.delete ('user_session')#Erase the whole key .#Create a folderR.set ('user:eee','haha')#Create a file to add a string type keyR.set ('user:ooo','hehe') R.hset ('Session:qwert','www','12345')#Create a file add hash type key

Python Foundation six-operational 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.