数据库操作

使用pymysql 模块可以对数据库进行链接 操作。

https://pypi.org/project/PyMySQL/

安装

  1. pip install pymysql

image.png

连接数据库

数据库连接信息

  1. 主机地址:49.233.108.117
  2. 端口号:3306
  3. 用户名:root
  4. 密码:fanmao888
  5. 数据库:mall

使用python的方式进行数据库连接。

  1. # 导入模块
  2. import pymysql
  3. # 连接数据库
  4. connections = pymysql.connect(
  5. host='49.233.108.117',
  6. port=3306,
  7. user='root',
  8. password='fanmao888',
  9. database='mall'
  10. )
  11. with connections.cursor() as cursor:
  12. # 编写查询语句
  13. sql = "select * from tb_newbee_mall_user;"
  14. cursor.execute(sql)
  15. # 查看一条结果
  16. # result = cursor.fetchone()
  17. # 查看所有的结果
  18. result2 = cursor.fetchmany()
  19. # print(result)
  20. print(result2)