python-mysql协程并发常用操作封装

本文介绍了一个使用Python编写的AsyncMySQLClient类,该类封装了aiomysql库,用于异步执行MySQL操作,包括连接、断开、非查询和查询语句,以及插入数据。并通过测试代码展示了如何使用这个封装后的客户端进行数据库操作。
摘要由CSDN通过智能技术生成


前言

协程异步操作MYSQL是常用的,博主这里在GitHub上找了两个包,databases和aiomysql,第一个包除了mysql外还支持其他的数据库,且操作MYSQL时底层也是使用的aiomysql,但文档内容比较少,所以选择了对aiomysql进行封装。使用

pip3 install aiomysql

安装即可。

封装代码

async_mysql.py

# coding:utf-8
import aiomysql
import traceback


class AsyncMySQLClient:
    def __init__(self, host: str, user: str, password: str, db: str, port: int = 3306, loop=None):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db = db
        self.loop = loop
        self.pool = None

    # 创建连接池
    async def connect(self):
        try:
            self.pool = await aiomysql.create_pool(
                host=self.host,
                port=self.port,
                user=self.user,
                password=self.password,
                db=self.db,
                loop=self.loop,
                autocommit=True
            )
        except:
            print(f"connect error:{traceback.format_exc()}")

    # 关闭连接池
    async def close(self):
        self.pool.close()
        await self.pool.wait_closed()

    # 执行一条非查询类语句
    async def execute(self, query:str, args:tuple=None) -> int:
        async with self.pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute(query, args)
                return cur.rowcount

    # 批量执行非查询类语句
    async def executemany(self, query:str, args:list=None) -> int:
        async with self.pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.executemany(query, args)
                return cur.rowcount

    # 查询单条数据
    async def fetchone(self, query:str, args:tuple=None) -> dict:
        async with self.pool.acquire() as conn:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute(query, args)
                return await cur.fetchone()

    # 查询多条数据
    async def fetchall(self, query:str, args=None) -> list:
        async with self.pool.acquire() as conn:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute(query, args)
                return await cur.fetchall()

    # 封装单条插入
    async def insert(self, table: str, data: dict) -> int:
        """
        :param table: 表名
        :param data: 数据
        :return:
        """
        keys = ','.join(data.keys())
        values = ','.join(['%s'] * len(data))
        query = f'INSERT INTO {table} ({keys}) VALUES ({values})'
        try:
            return await self.execute(query, tuple(data.values()))
        except:
            print(f"execute {query} with {data} failed, error{traceback.format_exc()}")
            return 0

    # 封装批量插入
    async def insert_many(self, table:str, data_list:list)->int:
        """
        :param table: 表名
        :param data_list: 数据列表
        :return:
        """
        keys = ','.join(data_list[0].keys())
        values = ','.join(['%s'] * len(data_list[0]))
        query = f'INSERT INTO {table} ({keys}) VALUES ({values})'
        args = [tuple(data.values()) for data in data_list]
        try:
            return await self.executemany(query, args)
        except:
            print(f"execute {query} with {args} failed, error{traceback.format_exc()}")
            return 0

非查询类的返回影响行数,查询类的返回数据,使用字典(或字典为元素的列表)。对于常用的插入和查询进行了封装,其他的使用execute函数即可。
使用时生成类的对象,之后connect,执行语句后close即可。

测试代码

import asyncio
import uuid
from async_mysql import AsyncMySQLClient


async def main():
   cli =  AsyncMySQLClient(host="127.0.0.1",user="root",password="12345678",db="test")
   await cli.connect()
   nums = await cli.insert(table="users",data={
       "id": uuid.uuid4(),
       "name": "lady_killer9"
   })
   print(f"插入{nums}条数据")
   random_id = uuid.uuid4()
   nums = await cli.insert_many(table="users",data_list=[{
       "id": random_id,
       "name": "lady_killer9"
   }])
   print(f"插入{nums}条数据")
   user = await cli.fetchone(query="select * from users where id = %s",args=(random_id,))
   print(f"查询到用户:{user}")
   users = await cli.fetchall(query="select * from users where name = 'lady_killer9'")
   print(f"所有用户:{users}")
   num = await cli.execute(query="update users set name='lady_killer8' where id = %s",args=(random_id,))
   if num == 1:
       print("修改成功")
   nums = await cli.execute(query="delete from users")
   print(f"删除{nums}条数据")
   await cli.close()

if __name__ == '__main__':
    asyncio.run(main())

截图如下:
在这里插入图片描述

参考

databases
aiomysql

更多python相关内容,参考:【python总结】python学习框架梳理

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lady_killer9

感谢您的打赏,我会加倍努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值