通过PyMySQL模块连接MySQL数据库 4.1 PyMySQL安装代码及运行结果截图。 4.2 PyMySQL模块连接MySQL数据库代码及运行结果截图。
时间: 2024-06-09 17:11:41 浏览: 131
4.1 PyMySQL安装代码及运行结果截图:
```python
!pip install PyMySQL
```
安装结果截图:

4.2 PyMySQL模块连接MySQL数据库代码及运行结果截图:
```python
import pymysql
# 连接数据库
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='123456',
database='test'
)
# 创建游标对象
cursor = conn.cursor()
# 执行SQL语句
cursor.execute('SELECT * FROM student')
# 获取查询结果
result = cursor.fetchall()
# 输出结果
for row in result:
print(row)
# 关闭游标对象
cursor.close()
# 关闭数据库连接
conn.close()
```
运行结果截图:

阅读全文
相关推荐

















