When to close cursors using MySQLdb
http://stackoverflow.com/questions/5669878/when-to-close-cursors-using-mysqldb
|
I'm building a WSGI web app and I have a MySQL database. I'm using MySQLdb, which provides cursors for executing statements and getting results. What is the standard practice for getting and closing cursors? In particular, how long should my cursors last? Should I get a new cursor for each transaction? I believe you need to close the cursor before committing the connection. Is there any significant advantage to finding sets of transactions that don't require intermediate commits so that you don't have to get new cursors for each transaction? Is there a lot of overhead for getting new cursors, or is it just not a big deal? |
|
| 4 Answers 4 |
|
Instead of asking what is standard practice, since that's often unclear and subjective, you might try looking to the module itself for guidance. In general, using the As of version 1.2.5 of the module,
There are several existing Q&A about
The question now is, what are the states of the connection and the cursor after exiting the
You should see the output "cursor is open; connection is open" printed to stdout.
Why? The MySQL C API, which is the basis for
I expect that's as close as you're going to get to "standard practice" on this subject.
I very much doubt it, and in trying to do so, you may introduce additional human error. Better to decide on a convention and stick with it.
The overhead is negligible, and doesn't touch the database server at all; it's entirely within the implementation of MySQLdb. You can look at Going back to earlier when we were discussing If it's really that important to you to micromanage the cursor object, you can use contextlib.closing to make up for the fact that the cursor object has no defined
Note that First, if autocommit mode is enabled, MySQLdb will Second,
You can test this by monitoring open connections (in Workbench or by using
|
|||||||||||||||||
|
- Uninteresting
- Misleading
- Offensive
- Repetitive
- Other

|
It's better to rewrite it using 'with' keyword. 'With' will take care about closing cursor (it's important because it's unmanaged resource) automatically. The benefit is it will close cursor in case of exception too.
|
|||||||||||||||||
|
|
I think you'll be better off trying to use one cursor for all of your executions, and close it at the end of your code. It's easier to work with, and it might have efficiency benefits as well (don't quote me on that one).
The point is that you can store the results of a cursor's execution in another variable, thereby freeing your cursor to make a second execution. You run into problems this way only if you're using fetchone(), and need to make a second cursor execution before you've iterated through all results from the first query. Otherwise, I'd say just close your cursors as soon as you're done getting all of the data out of them. That way you don't have to worry about tying up loose ends later in your code. |
|||||||||||||||||
|
|
I suggest to do it like php and mysql. Start i at the beginning of your code before printing of the first data. So if you get a connect error you can display a |
|||||||||
|
When to close cursors using MySQLdb的更多相关文章
- MySQLdb安装和使用2
http://blog.chinaunix.net/uid-8487640-id-3183185.html MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: ...
- pyhon MySQLdb查询出来的数据设置为字典类型
import MySQLdbimport MySQLdb.cursors cxn=MySQLdb.Connect(host='localhost',user='root',passwd='1234', ...
- 解决mysqldb查询大量数据导致内存使用过高的问题
1.源码 connection=MySQLdb.connect( host="thehost",user="theuser", passwd="the ...
- python使用MySQLdb实现连接数据库Mysql
python实现连接数据库mysql的步骤: 一.引入MySQLdb 二.获取与数据库的连接 三.执行SQL语句和存储过程 四.关闭数据库连接 1.什么是MySQLdb? MySQLdb是用于pyth ...
- 【mysql】MySQLdb返回字典方法
来源:http://blog.csdn.net/zgl_dm/article/details/8710371 默认mysqldb返回的是元组,这样对使用者不太友好,也不利于维护下面是解决方法 impo ...
- python MySQLdb安装和使用
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-py ...
- 基于python3.x,使用Tornado中的torndb模块操作数据库
目前Tornado中的torndb模块是不支持python3.x,所以需要修改部分torndb源码即可正常使用 1.开发环境介绍 操作系统:win8(64位),python版本:python3.6(3 ...
- 连接数据库-stone
# -*- coding:utf-8 -*- import pymysql class mysql: def __init__(self, host, port, dbuser, dbpwd, dbn ...
- python mysql基本操作
1.创建数据库.表添加数据. # -*- coding: utf-8 -*- import MySQLdb.cursors conn =MySQLdb.connect(',charset = 'utf ...
随机推荐
- Redis数据库的使用场景介绍(避免误用Redis)
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/122.html?1455854235 Redis 是目前 NoSQL 领域 ...
- 删除顽固node_modules
在工作中有用到gulp,webpack,使用他们需用依赖node的一些模块包,于是会在目录下生成一个node_modules文件夹.有一次想删掉它重新生成模块包的时候发现根本不太可能,无穷无尽的报一个 ...
- 合法提交Html标签 Page指令
3.2.1 提交合法的HTML标签(1) 有时候我们需要让我们提交的文本展示出来的效果非常美观,通常会对服务器提交一些HTML标签来控制文本或内容的样式. HTML标签可能包含了很多不安全的因素,所以 ...
- 3D打印:三维智能数字化创造(全彩)
3D打印:三维智能数字化创造(全彩)(全球第一本系统阐述3D打印与3D智能数字化的专业著作) 吴怀宇 编 ISBN 978-7-121-22063-0 2014年1月出版 定价:99.00元 42 ...
- python学习 流程控制语句
##################################### 分支语句 python3.5 #########################################代码的缩进格 ...
- Jasmine入门(上)
什么是Jasmine Jasmine是一个Javascript的BDD(Behavior-Driven Development)测试框架,不依赖任何其他框架. 如何使用Jasmine 从Github上 ...
- Linux快速入门03-系统管理
这部分将涉及常用的各类linux命令和一些系统高级管理特性,尤其是shell script的创建,这部分在系统自动化运维时会很有作用. Linux系列文章 快速入门系列--Linux--01基础概念 ...
- vs xamarin android 读取rest
private void Btn_Click(object sender, EventArgs e) { var u = FindViewById<EditText>(Resource.I ...
- 使用Nginx配置NodeJs程序(Windows平台)
简介 Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 服务器. Nginx 是由 Igor Sysoev ...
- 构建自己的PHP框架--抽象Controller的基类
上一篇博客中,我们将简单的路由解析和执行,从入口文件public/index.php中移入到框架中.入口文件顿时变得清爽无比-- 但是,去我们的controller里看一下,会看到如下的code: p ...


