目录
可以尝试一下VSCode的ChatGPT插件
一、Python3连接MySQL数据库
- Windows下打开服务管理器的命令:services.msc
- Windows下MySQL8.0 starting the server失败解决办法: https://blog.csdn.net/LYD203/article/details/122269317
- Ubuntu22.04安装MySQL8可以参考: https://blog.csdn.net/c_learner_/article/details/125238004
- Python3连接MySQL数据库可参考: https://www.runoob.com/python3/python3-mysql.html
安装PyMySQL 驱动
1. 查询
获取结果集的函数:
- fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
- fetchall(): 接收全部的返回结果行.
- rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
其他说明:
- 一般在退出数据库连接后需要对连接和cursor对向完成关闭,释放资源。
- DB-API的主要函数
connect() 创建数据库连接。
cursor() 管理查询
execute()和executemany() 运行一个或多个SQL命令
fetchone()、fetchmany()和fetchall() 获取execute()的结果
2. 修改
3. 新增
为了防止SQL注入攻击,可以使用占位符(placeholder)来形成SQL语句。
在这个示例中,%s 被用作占位符,实际的值通过 cursor.execute 方法的第二个参数传递。这样可以确保参数被正确地转义,从而防止SQL注入攻击。确保占位符的数量与传递的参数数量一致,并且类型匹配。
4. 删除
二、异步方法连接MySQL数据库
模块aiomysql基于PyMySQL,提供与PyMySQL相同的API,但具备异步能力。
官网: https://aiomysql.readthedocs.io/en/stable/index.html
安装aiomysql
使用的基本例子
三、asyncio并发应用
asyncio 是 Python 3.4 引入的标准库之一,用于编写异步代码。如果您使用的是 Python 3.4 以上版本,则不需要额外安装 asyncio,因为它已经是 Python 的一部分。
- 批量并发ping
安装软件包
代码如下:
四、python3使用ssh连接主机
安装软件包
示例代码如下:
引入多进程执行命令的例子——加快命令执行速度