上下文管理器连接数据库示例
2021/11/30 19:08:45
本文主要是介绍上下文管理器连接数据库示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import pymysql class DB_LINK(): def __init__(self): self.pool_db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test', charset='utf8', cursorclass=pymysql.cursors.DictCursor) def __enter__(self): self.coon = self.pool_db.cursor() return self.coon def __exit__(self, exc_type, exc_val, exc_tb): print(exc_type)#异常类型 print(exc_val)#异常实例 print(exc_tb)#回溯选择 if exc_val: self.coon.close() print('关闭数据库了') return False with DB_LINK() as db: db.execute('select * from user where age<2 and username="qxh" and id<500') print(db.fetchall())
[{'id': 52, 'username': 'qxh', 'age': 1}, {'id': 128, 'username': 'qxh', 'age': 1}, {'id': 134, 'username': 'qxh', 'age': 1}, {'id': 229, 'username': 'qxh', 'age': 1}, {'id': 250, 'username': 'qxh', 'age': 1}, {'id': 426, 'username': 'qxh', 'age': 1}] None None None
故意修改sql语句使其报错
with DB_LINK() as db: db.execute('selecct * from user where age<2 and username="qxh" and id<500') print(db.fetchall())
报错内容:
Traceback (most recent call last): File "C:/Users/Administrator/Desktop/test/mysql_pool_test.py", line 113, in <module> db.execute('selecct * from user where age<2 and username="qxh" and id<500') File "D:\virtualenv\py3\lib\site-packages\pymysql\cursors.py", line 148, in execute result = self._query(query) File "D:\virtualenv\py3\lib\site-packages\pymysql\cursors.py", line 310, in _query conn.query(q) File "D:\virtualenv\py3\lib\site-packages\pymysql\connections.py", line 548, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:\virtualenv\py3\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result result.read() File "D:\virtualenv\py3\lib\site-packages\pymysql\connections.py", line 1156, in read first_packet = self.connection._read_packet() File "D:\virtualenv\py3\lib\site-packages\pymysql\connections.py", line 725, in _read_packet packet.raise_for_error() File "D:\virtualenv\py3\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error err.raise_mysql_exception(self._data) File "D:\virtualenv\py3\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'selecct * from user where age<2 and username="qxh" and id<500\' at line 1') <class 'pymysql.err.ProgrammingError'> (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'selecct * from user where age<2 and username="qxh" and id<500\' at line 1') <traceback object at 0x00000240B011A580> 关闭数据库了
终止异常:__exit__:return True
<class 'pymysql.err.ProgrammingError'> (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'selecct * from user where age<2 and username="qxh" and id<500\' at line 1') <traceback object at 0x00000296676E9380> 关闭数据库了
这篇关于上下文管理器连接数据库示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11有哪些好用的家政团队管理工具?
- 2025-01-11营销人必看的GTM五个指标
- 2025-01-11办公软件在直播电商前期筹划中的应用与推荐
- 2025-01-11提升组织效率:上级管理者如何优化跨部门任务分配
- 2025-01-11酒店精细化运营背后的协同工具支持
- 2025-01-11跨境电商选品全攻略:工具使用、市场数据与选品策略
- 2025-01-11数据驱动酒店管理:在线工具的核心价值解析
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API