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 ...
随机推荐
- VS2012 MVC4 学习笔记-概览
1. 访问请求过程 访问收到后路由(Router)根据路径由分配给对应的控制器(Control),然后由控制器返回页面视图(View) 路由设置一个默认的控制器,类似 主页的样子吧 <未完待续& ...
- 使用BCP导出导入数据
bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容量复制数据. 使用 bcp 实用工具可以将大量新行导入 SQL Server 表,或将表数据导出到数 ...
- 纯CSS制作水平垂直居中“十字架”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- OpenCascade BRep Format Description
OpenCascade BRep Format Description eryar@163.com 摘要Abstract:本文结合OpenCascade的BRep格式描述文档和源程序,对BRep格式进 ...
- CSS布局(上)
CSS布局(上) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !import ...
- Java多线程系列--“基础篇”07之 线程休眠
概要 本章,会对Thread中sleep()方法进行介绍.涉及到的内容包括:1. sleep()介绍2. sleep()示例3. sleep() 与 wait()的比较 转载请注明出处:http:// ...
- 可视化工具gephi源码探秘(一)
今天在老大和小梁的鼓舞和忽悠下(^_^),我决定还是把之前下载好的gephi源码好好利用起来,不在朝三暮四的想d3js或是什么vizster,用心去选择一个自己熟悉的,而不是一直在各种困难之间来回徘徊 ...
- Windows Azure Web Site (12) Azure Web Site配置文件
<Windows Azure Platform 系列文章目录> 本文将介绍如何在Azure Web Site里配置连接字符串. 本文分为以下几个步骤: 1.在本地ASP.NET项目使用W ...
- SQL Server 2016的数据库范围内的配置
SQL Server 2016真的让人眼前一亮.几天前微软就提供了RCO(候选发布版)版本的下载.我已经围观了一圈RCO版本,其中一个最拽的功能是数据库范围内的配置(Database Scoped C ...
- 搭建 Hexo Blog
一.教程参考:https://xuanwo.org/2015/03/26/hexo-intor/ 二.命令总结 1.node -v.npm -v:查看 node.js 是否安装成功 2.npm ins ...


