python与mysql,redis交互

一、python与mysql交互

官网:

https://pypi.org/project/PyMySQL/

socker

cs 架构 client 客户端 server 服务端

支持

  • python 2.7 3.4 +
  • mysql server 5.5+

安装:

  • pip install pymysql==0.9.3

介绍:

在python中用pymysql 模块对mysql进行操作,该模块本质就是一个套接字客户端软件

连接前的操作

  • 设置端口映射 mysql 默认端口 3306
  • 使用 admin 用户 Root110qwe

方法

connection 对象

创建对象 conn = connect(参数列表)

  • host
  • port
  • db
  • user
  • password
  • charset

对象方法

commit 提交

cursor 对象 用于执行sql语句 并获得结果 cs = conn.cursor()

close 关闭连接

cursor 对象

execute() 执行语句 insert select update delete

fetchone () 返回结果 只返回第一个行数据

fetchall() 返回所有 元组对象

基本操作
1、 案列一:模板
    import pymysql
    
    # 建立连接
    conn = pymysql.connect(
        host='127.0.0.1',     # 地址
        port=3306,             # 端口
        user='admin',           # 用户名
        password='Root110qwe',  # 密码
        db='python50',          # 库名
        charset='utf8'          # 编码格式
    )
    
    # 2、创建游标  获取connection 的 cursor() 方法
    cursor = conn.cursor()
    
    # 执行 sql语句
    cursor.execute('select * from students')
    
    # 获取结果
    
    res = cursor.fetchall()   # 获取所有
    print(res)
    
    # 关闭游标
    cursor.close()
    
    # 关闭连接
    conn.close()
2、 案列二: 指定数据返回
    import pymysql
    
    # 建立连接
    conn = pymysql.connect(
        host='127.0.0.1',     # 地址
        port=3306,             # 端口
        user='admin',           # 用户名
        password='Root110qwe',  # 密码
        db='python50',          # 库名
        charset='utf8'          # 编码格式
    )
    
    # 2、创建游标  获取connection 的 cursor() 方法
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    
    # 执行 sql语句
    cursor.execute('select * from students')
    
    # 获取结果
    # 返回一条  first
    res1 = cursor.fetchone()
    print(res1)
    print('返回第一条数据')
    res2 = cursor.fetchmany(2)
    print(res2)
    print('返回两条数据')
    res = cursor.fetchall()   # 获取所有
    print(type(res),res)
    
    # 关闭游标
    cursor.close()
    
    # 关闭连接
    conn.close()
3、案列3: 增删改操作
    # import pymysql
    from pymysql import *
    def main():
        # 创建一个连接
        conn = connect(
            host='127.0.0.1',  # 地址
            port=3306,  # 端口
            user='root',  # 用户名
            password='qwe123',  # 密码
            db='python50',  # 库名
            charset='utf8'  # 编码格式
        )
        # 获取CURSOR对象
        cs1 = conn.cursor(cursor=cursors.DictCursor)
        # 执行操作语句  execute()
        count = cs1.execute('insert into works VALUES (null,"喵星人",18,2,168)')
        print(count)
        # 指定字段插入
        count = cs1.execute('insert into works(name) VALUES ("泰迪")')
        print(count)
        # 更新数据
        cu = cs1.execute('update works set name="雪橇犬" where name="中华田园犬"')
        # 删除
        count = cs1.execute('delete from works where id=43')
        print(count)
    
    
        # 查询语句
        cs1.execute('select * from works')
        res = cs1.fetchall()   # 返回所有
        # res = cs1.fetchone()   # 返回所有
        print(res)
    
        # 提交操作 如果之前执行多次的execute 语句, 那么就都要进行提交
        conn.commit()
    
        # 关闭游标
        cs1.close()
        # 关闭连接
        conn.close()
    
    
    if __name__ == '__main__':
        main()
4、案列4 :参数化
    import pymysql
    
    def main():
        find_name = input('请输入宠物名字:')
        conn = pymysql.connect(
            host='127.0.0.1',  # 地址
            port=3306,  # 端口
            user='root',  # 用户名
            password='qwe123',  # 密码
            db='python50',  # 库名
            charset='utf8'  # 编码格式
        )
    
        cs1 = conn.cursor()
    
        # 构建参数列表
        params = [find_name]
    
        # 执行select 语句
        count = cs1.execute('select * from works where name=%s', params)
        print(count)
    
        res = cs1.fetchall()
        print(res)
    
        cs1.close()
        conn.close()
    
    if __name__ == '__main__':
        main()

二、python与redis交互

1、 redis-py 介绍

是键值数据库redis的python接口

支持

  • python2.7 python3.4 +

安装

  • pip install redis

说明

需要端口运算 6379 ----- 6379

基本操作
案列1
    import redis
    
    # conn = redis.Redis(host='127.0.0.1',port=6379,db=10)
    conn = redis.StrictRedis(
    						host='127.0.0.1',
    						port=6379,
    						db=10,
    						decode_responses=True
    				)
    # key 操作
    res = conn.keys('*')
    print(res)
    a = []
    for i in res:
        a.append(i.decode())
    print(a)
    
    print(conn.keys('name'))
    print(conn.delete('name'))
    
    
    # 设置键和值
    
    # string 操作
    res1 = conn.set('name','hello world')
    # 查询数据
    res2 = conn.get('name')
    print(res2)
    
    # hash 操作  设置键 属性 值
    conn.hset('halo','age','18')
    # 获取值
    res3 = conn.hget('halo','age')
    print(res3)
    
    
    # redis 管道
    # 管道  减少连接 降低损耗
    pipe = conn.pipeline()   # 创建redis 连接
    
    pipe.keys()   #  返回所有键
    pipe.set('name','大家晚上好')
    pipe.get('name')
    
    res = pipe.execute()    # 执行管道
    print(res)
    print(res[0])
    print(res[1])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值