Python Operations Database (MySQL, Redis)

Source: Internet
Author: User
Tags redis name database install redis

Tag: res color cursor specifies the character name database exists select

1, Python3 operation MySQL database needs to install a third-party module (PYMYSQL): Pip install pymysql; Operation Redis requires the installation of a Redis module (redis): Pip install Redis

Python operation MySQL:

Import Pymysql #导入模块
# conn =pymysql.connect (host= ' 211.149.147.233 ', user= ' Byz ', passwd= ' 123456 ', db= ' Byz ', port=3306,charset= ' UTF8 ') # Create a database link, specify IP, account password, port, database name, character set
# cur = conn.cursor (cursor=pymysql.cursors.dictcursor) #创建游标, specifying the output data type
# cur= Conn.cursor (cursor=pymysql.cursors.dictcursor)
# sql = ' INSERT INTO user ' (Id,username,password) value ("Test", "123456"); '
# SQL1 = ' select * from user; '
# Cur.execute (SQL1) #执行sql
# Print (Cur.fetchall ()) #获取SQL结果所有数据
# Print (Cur.fetchone ()) #获取SQL结果一条数据
# Print (Cur.fetchone ()) #获取SQL结果一条数据
# Cur.scroll (4,mode= ' absolute ') #和fetchone结合使用, move position to fifth (number of positions starting from 0)--absolute position
# cur.scroll (4,mode= ' relative ') #和fetchone结合使用, move position backward 4 (relative position)
# Print (Cur.fetchone ()) #结合absolute, this shows the sixth data in the database
# conn.commit () #提交执行 Insert, delete, update must be submitted to take effect
# cur.close () #关闭游标
# conn.close () #关闭数据库链接

# Mysql_info = {
# "host": ' 211.149.147.233 ',
# "User": ' Byz ',
# "passwd": ' 123456 ',
# "DB": ' Byz ',
# "Port": 3306,
# "CharSet": ' UTF8 '
# }
#链接MySQL的函数
# def openmysql (SQL):
# host=mysql_info[' Host '
# user=mysql_info[' user '
# passwd=mysql_info[' passwd ')
# db=mysql_info[' db ')
# port=mysql_info[' Port '
# charset=mysql_info[' CharSet ')
# conn = Pymysql.connect (Host=host,user=user,passwd=passwd,db=db,port=port,charset=charset)
# cur= Conn.cursor (cursor=pymysql.cursors.dictcursor)
# Cur.execute (SQL)
# If Sql.startswith (' select '):
# res = Cur.fetchall ()
# Else:
# Conn.commit ()
# res = ' Commit success '
# Cur.close ()
# Conn.close ()
# return Res
# sql = ' INSERT INTO user ' (Id,username,password) value ("Test", "123456"); '
# sql = ' select * from user; '
#调用函数
rr = Openmysql (Sql=sql)
Print (RR)


# python operation Redis
Import Redis,json
# r = Redis. Redis (host= ' 211.149.218.16 ', port=6379,db=0,password= ' 123456 ') #链接redis
# r.set (' name ', ' jiameiyu1 ') #设置name的值, if name does not exist, create a new one, or modify the value of name
# r.setnx (' name ', ' Jiameiyu ') #设置name的值, the name does not exist when it is set
# R.setex (' name1 ', ' Jiameiyu ', #设置name的值), more than 15s is void
# R.mset (name2= ' name2 ', name3= ' Name3 ', name4= ' name4 ') #批量设置值
# Print (r.mget (' name ', ' name1 ', ' name2 ', ' Name3 ')) #批量获取key值
# r.delete (' name ') #删除值
# r.delete (' name2 ', ' Name3 ') #批量删除
#操作哈希类型的值
# r.hset (' Hname ', ' key ', ' value ')
# r.hsetnx (' Hname ', ' key2 ', ' value2 ') #给hname设置key和value值, set when key does not exist
# r.hmset (' Hname ', {' K1 ': ' v1 ', ' K2 ': ' V2 '}) #给hname批量设置key value
# Print (R.hget (' hname ', ' key ')) #获取这个hname里面指定的key值
# Print (R.hgetall (' Hname ')) #获取所有的key

# Redis_info = {
# "host": ' 211.149.218.16 ',
# "passwd": ' 123456 ',
# "DB": 0,
# "Port": 6379
# }
# Functions for working with Redis
# def Opredis (Host,passwd,k,port=3679,db=0,v=false):
# r = Redis. Redis (HOST=HOST,PASSWORD=PASSWD,PORT=PORT,DB=DB) #链接redis
# if V: #如果v为空则调set, otherwise call get
# R.set (k,v)
# res = ' Set Success '
# Else:
# res = R.get (k). Decode () #返回获取值, where you use decode to convert the byte bytes returned by the Redis default to a string
# return Res
# #调用函数
# res = Opredis (
# host=redis_info[' Host ',
# passwd=redis_info[' passwd '),
# db=redis_info[' db '),
# port=redis_info[' Port '),
# k= ' Zs '
# )
# Print (RES)











Python Operations Database (MySQL, 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.