当前位置:   article > 正文

python连接mysql之PyMySQL的基本使用_pymysql charset

pymysql charset

一、PyMySQL的基本使用

使用pymysql 直接连接mysql

PyMySQL安装

pip3 install pymysql
  1. import pymysql
  2. # 连接数据库,创建连接对象connection
  3. # 连接对象作用是:连接数据库、发送数据库信息、处理回滚操作(查询中断时,数据库回到最初状态)、创建新的光标对象
  4. conn = pymysql.connect(host='127.0.0.1', # host属性
  5. port=3308, # 端口号
  6. user='root', # 用户名
  7. password='123456', # 此处填登录数据库的密码
  8. db='bt' , # 数据库名
  9. charset=“utf8”
  10. )
  11. # 得到一个可以执行SQL语句的光标对象
  12. cursor = conn.cursor()
  13. # 执行完毕返回的结果集默认以元组显示
  14. sql = """ """
  15. # 执行SQL语句
  16. cursor.execute(sql)
  17. # 关闭光标对象
  18. cursor.close()
  19. # 关闭数据库连接
  20. conn.close()

二、增删改查操作

2.2 添加多条数据

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test2.py
  5. import pymysql
  6. conn = pymysql.connect(host='127.0.0.1', # host属性
  7. port=3308, # 端口号
  8. user='root', # 用户名
  9. password='123456', # 此处填登录数据库的密码
  10. db='bt' # 数据库名
  11. )
  12. # 获取一个光标
  13. cursor = conn.cursor()
  14. # 定义要执行的sql语句
  15. sql = 'insert into student(name,age) values(%s,%s);'
  16. data = [
  17. ('july', 14),
  18. ('june', 25),
  19. ('marin', 36)
  20. ]
  21. # 拼接并执行sql语句
  22. cursor.executemany(sql, data)
  23. # 涉及写操作要注意提交
  24. conn.commit()
  25. # 关闭连接
  26. cursor.close()
  27. conn.close()

2.3 插入单条数据

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test3.py
  5. import pymysql
  6. conn = pymysql.connect(host='127.0.0.1', # host属性
  7. port=3308, # 端口号
  8. user='root', # 用户名
  9. password='123456', # 此处填登录数据库的密码
  10. db='bt' # 数据库名
  11. )
  12. cursor = conn.cursor() # 获取一个光标
  13. sql = 'insert into student (name,age) values (%s,%s);'
  14. name = 'wuli'
  15. age = 10
  16. cursor.execute(sql, [name, age])
  17. conn.commit()
  18. cursor.close()
  19. conn.close()

2.4 获取最新插入数据 (最后一条)

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test4.py
  5. import pymysql
  6. # 建立连接
  7. conn = pymysql.connect(host='127.0.0.1', # host属性
  8. port=3308, # 端口号
  9. user='root', # 用户名
  10. password='123456', # 此处填登录数据库的密码
  11. db='bt' # 数据库名
  12. )
  13. # 获取一个光标
  14. cursor = conn.cursor()
  15. # 定义将要执行的SQL语句
  16. sql = 'insert into student (name,age) values (%s,%s);'
  17. name = 'wuli'
  18. age = 10
  19. cursor.execute(sql, [name, age])
  20. conn.commit()
  21. # 关闭连接
  22. # 获取最新的那一条数据的ID
  23. last_id = cursor.lastrowid
  24. print("最后一条数据的ID是:", last_id)
  25. cursor.close()
  26. conn.close()

2.5 删除操作

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test5.py
  5. import pymysql
  6. # 建立连接
  7. conn = pymysql.connect(host='127.0.0.1', # host属性
  8. port=3308, # 端口号
  9. user='root', # 用户名
  10. password='123456', # 此处填登录数据库的密码
  11. db='bt' # 数据库名
  12. )
  13. # 获取一个光标
  14. cursor = conn.cursor()
  15. # 定义将要执行的SQL语句
  16. sql = "delete from student where name=%s;"
  17. name = "wuli"
  18. # 拼接并执行SQL语句
  19. cursor.execute(sql, [name])
  20. # 涉及写操作注意要提交
  21. conn.commit()
  22. # 关闭连接
  23. cursor.close()
  24. conn.close()

2.5 更改数据

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test6.py
  5. import pymysql
  6. # 建立连接
  7. conn = pymysql.connect(host='127.0.0.1', # host属性
  8. port=3308, # 端口号
  9. user='root', # 用户名
  10. password='123456', # 此处填登录数据库的密码
  11. db='bt' # 数据库名
  12. )
  13. # 获取一个光标
  14. cursor = conn.cursor()
  15. # 定义将要执行的SQL语句
  16. sql = "update student set age=%s where name=%s;"
  17. # 拼接并执行SQL语句
  18. cursor.execute(sql, [28,"ni"])
  19. # 涉及写操作注意要提交
  20. conn.commit()
  21. # 关闭连接
  22. cursor.close()
  23. conn.close()

三、查询数据

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test7.py
  5. import pymysql
  6. # 建立连接
  7. conn = pymysql.connect(host='127.0.0.1', # host属性
  8. port=3308, # 端口号
  9. user='root', # 用户名
  10. password='123456', # 此处填登录数据库的密码
  11. db='bt', # 数据库名
  12. charset='utf8'
  13. )
  14. # 获取一个光标
  15. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 返回字典数据类型
  16. # 定义将要执行的sql语句
  17. sql = 'select name,age from student'
  18. # 拼接并执行sql语句
  19. cursor.execute(sql)
  20. # 取到查询结果
  21. ret1 = cursor.fetchone() # 取一条
  22. ret2 = cursor.fetchmany(3) # 取三条
  23. ret3 = cursor.fetchone() # 取一条
  24. cursor.close()
  25. conn.close()
  26. print(ret1)
  27. print(ret2)
  28. print(ret3)

打印结果:

  1. {'name': 'ni', 'age': 28}
  2. [{'name': 'wo', 'age': 13}, {'name': 'july', 'age': 14}, {'name': 'june', 'age': 25}]
  3. {'name': 'marin', 'age': 36}

注意:

ret1 = cursor.fetchone() # 取一条

ret2 = cursor.fetchmany(3) # 取三条

ret3 = cursor.fetchone() # 取一条

这里的取一条,不是第一条,是依次往下取数据的,ret1 ,是第1条数据,ret2 ,是 2、3、4 数据。

ret3 是第5条数据。

四、数据回滚

  1. # -*- coding:utf-8 -*-
  2. # @Author: 喵酱
  3. # @time: 2022 - 06 -18
  4. # @File: test8.py
  5. import pymysql
  6. # 建立连接
  7. conn = pymysql.connect(host='127.0.0.1', # host属性
  8. port=3308, # 端口号
  9. user='root', # 用户名
  10. password='123456', # 此处填登录数据库的密码
  11. db='bt', # 数据库名
  12. charset='utf8'
  13. )
  14. # 获取一个光标
  15. cursor = conn.cursor()
  16. # 定义将要执行的SQL语句
  17. sql1 = "insert into student (name, age) values (%s, %s);"
  18. sql2 = "insert into hobby (id, hobby) values (%s,%s);"
  19. name = "july1"
  20. age = 14
  21. id = "我是错误的id" # id = "3"
  22. hobby = "打游戏"
  23. try:
  24. # 拼接并执行SQL语句
  25. cursor.execute(sql1, [name, age])
  26. print(sql1)
  27. cursor.execute(sql2, [id, hobby]) # 报错的SQL语句
  28. # 涉及写操作注意要提交
  29. conn.commit()
  30. except Exception as e:
  31. print(str(e))
  32. # 有异常就回滚
  33. conn.rollback()
  34. # 关闭连接
  35. cursor.close()
  36. conn.close()

发生异常后,2条数据,都没有写入库里。

六、python通过连接池连接数据库

6.1初始化 __init__

  1. def __init__(self, **kwargs):
  2. self.size = kwargs.get('size', 10)
  3. self.kwargs = kwargs
  4. self.conn_queue = queue.Queue(maxsize=self.size)
  5. for i in range(self.size):
  6. self.conn_queue.put(self._create_new_conn())
  • size:连接池支持的连接数,这里定义为10

  • conn_queue:定义了一个队列,队列存放的是数据库的连接

  • for循环:建立好十个与数据库的连接,把这些连接放到队列中

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/634769
推荐阅读
相关标签
  

闽ICP备14008679号