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 ...
随机推荐
- 这里有个坑---js日期格式yyyy-MM-dd与yyyy/MM/dd
这里有个坑,---------每一个遇到的坑总结后都是一比财富. 我们写脚本的时候,一般定义一个日期格式会使用“2015-12-21”和“2015/12/21”两种数据格式,由于各取所需日期格式并没有 ...
- PLoP(Pattern Languages of Programs,程序设计的模式语言)
2014/8/1 12:24:21潘加宇 http://www.umlchina.com/News/Content/340.htmPloP大会2014即将举行 PLoP(Pattern Languag ...
- scikit-learn的梯度提升算法(Gradient Boosting)使用
前言:本文的目的是记录sklearn包中GBRT的使用,主要是官网各参数的意义:对于理论部分和实际的使用希望在只是给出出处,希望之后有时间能补充完整 摘要: 1.示例 2.模型主要参数 3.模型主要属 ...
- 我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器
回到目录 AOP面向切面的编程,也称面向方面的编程,我更青睐于前面的叫法,将一个大系统切成多个独立的部分,而这个独立的部分又可以方便的插拔在其它领域的系统之中,这种编程的方式我们叫它面向切面,而这些独 ...
- GridView和DATAGRID前后台查询用法的比较
Grideview前台: <DIV class="mainDiv" id="GridWidth"> <ASP:GridView id=&quo ...
- js 优化
一.for循环的优化 <!doctype html> <html lang="en"> <head> <meta charset=&quo ...
- PHP实现RESTful风格的API实例(一)
最近看了一些关于RESTful的资料,自己动手也写了一个RESTful实例,以下是源码 目录详情: restful/ Request.php 数据操作类 Response.php 输出类 index. ...
- hibernate(六)一对一映射
关系映射是指对象之间的关系,并不是指数据库的关系,关系映射是解决当对象处于以下关系之一时,数据库表该如何映射的问题 (一)一对一单向外键关联 1.注解方式配置 创建一个Husband类和Wife类 H ...
- [Node.js] 使用TypeScript编写Node项目
原文地址:http://www.moye.me/2015/04/25/using_typescript/ 入 TypeScript 坑有一段时间了,由衷的看好,所以决定在自留地絮叨一番. 是什么 Ty ...
- web接口开发与测试
最近一直在学习和整理web开发与接口测试的相关资料.接口测试本身毫无任何难度,甚至有很多工具和类库来帮助我们进行接口测试.大多测试人员很难深入了解web接口测试的原因是对web开发不太了解,当你越了解 ...