使用python中的pymysql库,并且转化为数组元组数据

目录

安装pymysql

编写read_mysql_data_by_pymsql.py

编写一个读取mysql,并且转化为元组的工具类


安装pymysql

  1. pip install pymysql

编写read_mysql_data_by_pymsql.py

  1. python 复制代码
    # 导入包
    import pymysql
    
    # 配置数据库连接数据
    mysql_ip_address ="127.0.0.1"
    mysql_user = "root"
    mysql_password = "root"
    mysql_database = "myst"
    mysql_port = 3306
    mysql_charset = "utf8"
    
    # sql语句
    sql_string = "select * from user"
    
    # 装填数据
    mysql_connect = pymysql.connect(
        host=mysql_ip_address,
        port=mysql_port,
        user=mysql_user,
        password=mysql_password,
        database=mysql_database,
        charset=mysql_charset,
    )
    
    # 获取游标
    cursor = mysql_connect.cursor()
    # 执行sql
    cursor.execute(sql_string)
    # 读取数据
    read_mysql_data =  cursor.fetchall()
    
    # 存放数据
    print(list(read_mysql_data))
    
    print(read_mysql_data)
  2. cursor.execute(sql) 执行完毕之后,然后使用list(result) 进行数据转化

  3. 运行结果

    1. 可以看到数据库中的数据已经成功读取了,并且转化为元组了

编写一个读取mysql,并且转化为元组的工具类

  1. python 复制代码
    # 导入包
    import pymysql
    
    # 配置数据库连接数据
    mysql_ip_address ="127.0.0.1"
    mysql_user = "root"
    mysql_password = "root"
    mysql_database = "myst"
    mysql_port = 3306
    mysql_charset = "utf8"
    query_sql = "select * from user"
    def read_mysql_data(host=mysql_ip_address,
                        port=mysql_port,
                        charset=mysql_charset,
                        user=mysql_user,
                        password=mysql_password,
                        database=mysql_database,
                        query_sql = query_sql):
    
        try:
            # 装填数据
            mysql_connect = pymysql.connect(
                host=host,
                port=port,
                user=user,
                password=password,
                database=database,
                charset=charset,
            )
    
            # 获取游标
            cursor = mysql_connect.cursor()
            # 执行sql
            cursor.execute(query_sql)
            # 读取数据
            mysql_from_data_list = cursor.fetchall()
            # 关闭游标
            cursor.close()
            # 关闭连接
            mysql_connect.close()
        except Exception as e:
            print(e)
        
        return list(mysql_from_data_list)
        
    
    print(read_mysql_data())
    1. 可以在工具类上面编写自己数据库的参数

    2. 也可以在使用工具时自动填写相关数据

    3. 复制代码
    4. 可以在使用的时候填入sql,这里就不在测试了

    5. 可以在进行执行的时候传入mysql的连接配置,也可以不传入,函数中传入的mysql配置优先级肯定是最高的,执行不同的sql需要从外部传入,这里默认写了一乐,这个是必须填写的参数。

      1. 这里使用了try-except捕捉异样
相关推荐
Ray Liang34 分钟前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮1 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling1 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮4 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽4 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健19 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞21 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python