1. python操作数据库 pymysql PyMSQL是一个纯的python的MySQL客户端。 pip install PyMySQL 使用步骤 创建连接(修高速公路) 创建游标(运输车) 执行sql(提货单) 获取结果(卸货) 关闭游标(车退掉) 关闭连接(路也断掉) import pymysql from pymysql.cursors import DictCursor # 1.创建连接(修高速公路) conn = pymysql.connect( host='127.0.0.1', user='admin', password='12345', port=3306, db='mysql', charset='utf8' ) # 2. 创建游标(运输车) # cursor = conn.cursor() # 返回元组 cursor = conn.cursor(DictCursor) # 返回字典 # 3. 执行sql(提货单) sql ="select count(1) total, (case when t1.status = 1 then '待整改' when t1.status = 2 then '待复查' when t1.status = 3 then '整改完成' else '未知类型' end) orderStatus from ibs_ai_iot.ai_rectification_main t1 left join ibs_ai_iot.work_order t3 on t1.id = t3.rectification_id where t1.project_id = 103672 and t1.delete_flag = 0 and t3.is_delete = 0 group by t1.status order by orderStatus desc;", res = cursor.execute(sql) print(res) # 4. 获取结果(卸货) res1 = cursor.fetchone() # 一次卸载一条 res2 = cursor.fetchmany(3) # 一次卸载指定的条数 res3 = cursor.fetchall() # 一次获取所有 # 5. 关闭游标(车退掉) cursor.close() # 6. 关闭连接(路也断掉) conn.close() [{'total': 30, 'orderStatus': '整改完成'}, {'total': 5, 'orderStatus': '待整改'}] 更新数据 import pymysql db_config = { 'host': '127.0.0.1', 'user': 'admin', 'password': '123456', 'port': 3306, 'db': 'mysql', 'charset': 'utf8' } conn = pymysql.connect(**db_config) try: with conn.cursor() as cursor: # 上下文管理--自动关闭游标 sql1 = 'UPDATE table t1 set t1.user_name="勇哥" where t1.username="h1" ' sql2 = 'UPDATE table t1 set t1.user_name="勇哥2" where t1.username="h2" ' cursor.execute(sql1) cursor.execute(sql2) # pymysql默认开启事务 conn.commit() except Exception as e: # 回滚 conn.rollback() finally: conn.close() 本栏目推荐文章Jmeter 如何连接mysql数据库?SQL Server 检测是不是数字型数据的两种方法如何通过Navicat把MySQL远程数据库导入到本地数据库和利时MACS6通过dat文件实现任意时间范围与测点的历史数据提取与科研分析深入理解spring框架:剖析多线程模式下数据库连接数据可视化是如何帮助大型企业提高效率的?redis 数据库openpyxl模块--------------------------------重复数据Django客户端应用1向服务端应用2发送POST请求并接收解析数据数据可视化是如何帮助我们高效了解数据的?pymysql 数据库 数据pymysql数据mysql clickhouse模块pymysql数据库 pymysql数据库 数据python 数据pymysql数据库python 数据 字段pymysql 数据库 数据 语句pymysql pymysql数据库 数据 游标pymysql情况 数据 数据openpyxl pymysql数据库 数据库 数据 模块pymysql