<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erics Tech Blog &#187; python</title>
	<atom:link href="http://eric.lubow.org/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Mon, 16 Aug 2010 12:30:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Philosophical Python</title>
		<link>http://eric.lubow.org/2010/python/philosophical-python/</link>
		<comments>http://eric.lubow.org/2010/python/philosophical-python/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 13:30:43 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[Poetry]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=552</guid>
		<description><![CDATA[Having spent a lot of time in the Perl world and even trying to write a Perl poem here and there, I decided to give some Python poetry a try. It turned out to be a bit more philosophical than poetic. But it compiles and that&#8217;s what counts; even though it doesn&#8217;t do anything useful [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2010%2Fpython%2Fphilosophical-python%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2010%2Fpython%2Fphilosophical-python%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Having spent a lot of time in the Perl world and even trying to write a <a href="http://eric.lubow.org/category/perl/poetry/">Perl poem</a> here and there, I decided to give some Python poetry a try.  It turned out to be a bit more philosophical than poetic. But it compiles and that&#8217;s what counts; even though it doesn&#8217;t do anything useful (or anything at all).<br />
<span id="more-552"></span><br />
Since as people, we get the most out of something by filling in small spaces with our imagination (think of how comic books are so successful), fill in the words that might be missing.  Either way, enjoy.  Hope you get as much from reading it as I did from writing it.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> people<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">class</span> Me<span style="color: black;">&#40;</span>being<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">mind</span> = MyThoughts<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">body</span> = MyBeing<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">soul</span> = MyEssence<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> inner<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers = <span style="color: #008000;">None</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mind_answers = <span style="color: #008000;">self</span>.<span style="color: black;">mind</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body_answers = <span style="color: #008000;">self</span>.<span style="color: black;">body</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; soul_answers = <span style="color: #008000;">self</span>.<span style="color: black;">soul</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers = <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span>mind_answers, body_answers, soul_answers<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> MeaningOfLife <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> answers:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> Awareness, <span style="color: #483d8b;">&quot;Searching for meaning&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers = <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>Intelligence, mind_answers, body_answers, soul_answers<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> answers<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span> Awareness, MeaningOfLife:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> MeaningOfLife</div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Permanent Link: Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='Permanent Link: When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/python/philosophical-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When To Use MySQL Cursor Classes In Python</title>
		<link>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/</link>
		<comments>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 15:00:40 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=526</guid>
		<description><![CDATA[I have been writing a lot of code that has been interacting with MySQL lately. Sometimes I find it easier to work the result set in a dictionary form and other times it is easier with an array. But in order to not break all your code, it is necessary to set a default cursor [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2010%2Fpython%2Fwhen-to-use-mysql-cursor-classes-in-python%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2010%2Fpython%2Fwhen-to-use-mysql-cursor-classes-in-python%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I have been writing a lot of code that has been interacting with MySQL lately.  Sometimes I find it easier to work the result set in a dictionary form and other times it is easier with an array.  But in order to not break all your code, it is necessary to set a default cursor class that keeps your code consistent.  More often than not, I find using using a arrays is easier since I just want quick access to all the retrieved data.  I also end up making my SELECT calls while specifying the columns and order of the columns I want returned.</p>
<p>The reason that using cursor classes is handy is because Python doesn&#8217;t come with a <strong>mysql_fetch_assoc</strong> like PHP or <strong>selectrow_hashref</strong> like Perl&#8217;s DBI interface.  Python uses cursor dictionaries to bridge this gap.  Ultimately your result is the same.  But as with Perl and PHP, defaulting to cursor dictionaries isn&#8217;t a good idea for larger datasets because of the extra processing time and memory required to convert the data.<br />
<span id="more-526"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<br />
conn = MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host=<span style="color: #483d8b;">'localhost'</span>, <span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'user'</span>,<br />
&nbsp; &nbsp; passwd=<span style="color: #483d8b;">'secret'</span>, db=<span style="color: #483d8b;">'test'</span><span style="color: black;">&#41;</span><br />
cursor = conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT this,that FROM foobar&quot;</span><span style="color: black;">&#41;</span><br />
rows = cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>This then allows me to do just call the DictCursor (dictionary based cursor) on a single cursor object for a particular query and leave all the rest of the queries as array based results.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb.<span style="color: black;">cursors</span><br />
<br />
conn = MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host=<span style="color: #483d8b;">'localhost'</span>, <span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'user'</span>,<br />
&nbsp; &nbsp; passwd=<span style="color: #483d8b;">'secret'</span>, db=<span style="color: #483d8b;">'test'</span><span style="color: black;">&#41;</span><br />
cursor = conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span>cursorclass=MySQLdb.<span style="color: black;">cursors</span>.<span style="color: black;">DictCursor</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT this,that FROM foobar&quot;</span><span style="color: black;">&#41;</span><br />
rows = cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> rows:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> row<span style="color: black;">&#91;</span><span style="color: #483d8b;">'this'</span><span style="color: black;">&#93;</span>, row<span style="color: black;">&#91;</span><span style="color: #483d8b;">'that'</span><span style="color: black;">&#93;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p><strong>Note:</strong> Doing something like changing the connection string to use a default cursor class of dictionary cursor in an existing code base is not a good idea.  It will make your results come back as dictionaries when your current code expects arrays.  Yes, I did this and had to track it down the hard way. Hopefully this saves you some trouble.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">conn = MySQLdb.<span style="color: black;">Connect</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; host=<span style="color: #483d8b;">'localhost'</span>, <span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'user'</span>,<br />
&nbsp; &nbsp; passwd=<span style="color: #483d8b;">'secret'</span>, db=<span style="color: #483d8b;">'test'</span>,<br />
&nbsp; &nbsp; cursorclass=MySQLdb.<span style="color: black;">cursors</span>.<span style="color: black;">DictCursor</span><span style="color: black;">&#41;</span></div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Permanent Link: Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/' rel='bookmark' title='Permanent Link: Database Read/Write Splitting in Frameworks/ORMs'>Database Read/Write Splitting in Frameworks/ORMs</a></li>
<li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Permanent Link: Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python Multiprocessing Pools and MySQL</title>
		<link>http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/</link>
		<comments>http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 12:00:00 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[multprocessing]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[pool]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=486</guid>
		<description><![CDATA[There really isn&#8217;t a solid Python module for multiprocessing and MySQL. Now this may be because MySQL on a single server is disk bound and therefore limited in speed or just because no one has written it. So here is a quick and dirty example using the Pool module in multiprocessing in Python 2.6 and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fpython%2Fpython-multiprocessing-pools-and-mysql%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fpython%2Fpython-multiprocessing-pools-and-mysql%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>There really isn&#8217;t a solid Python module for multiprocessing and MySQL.  Now this may be because MySQL on a single server is disk bound and therefore limited in speed or just because no one has written it.  So here is a quick and dirty example using the <strong>Pool</strong> module in <em>multiprocessing</em> in Python 2.6 and <a href="http://mysql-python.sourceforge.net/MySQLdb.html">MySQLdb</a>.</p>
<p>I also tried using <a href="http://code.google.com/p/pysqlpool/">PySQLPool</a>.  This was designed for threading and not forking as I am doing with Pool method.  Although I am sure it is possible to use PySQLPool with forking by passing the connection (pool) object down to the child process or possibly doing something with IPC, I decided to keep it simple (although slightly more expensive) and instantiate MySQLdb connections upon fork.<br />
<span id="more-486"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<span style="color: #ff7700;font-weight:bold;">import</span> multiprocessing<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; tablename = <span style="color: #483d8b;">'address_demographics'</span><br />
&nbsp; &nbsp; inserts = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">id</span> = <span style="color: #ff4500;">0</span> <br />
&nbsp; &nbsp; max_id = <span style="color: #ff4500;">1000000</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">id</span> <span style="color: #66cc66;">&lt;</span> max_id:<br />
&nbsp; &nbsp; &nbsp; &nbsp; sql = <span style="color: #483d8b;">&quot;&quot;&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; INSERT IGNORE INTO `%s` (person_id)<br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT person_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM `people`<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE id BETWEEN %d AND %d<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>tablename,<span style="color: #008000;">id</span>,<span style="color: black;">&#40;</span><span style="color: #008000;">id</span> + <span style="color: #ff4500;">100000</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; inserts.<span style="color: black;">append</span><span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">id</span> += <span style="color: #ff4500;">100001</span><br />
&nbsp; &nbsp; pool = multiprocessing.<span style="color: black;">Pool</span><span style="color: black;">&#40;</span>multiprocessing.<span style="color: black;">cpu_count</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pool.<span style="color: #008000;">map</span><span style="color: black;">&#40;</span>sqlWorkerInsert, inserts, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pool.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pool.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> sqlWorkerInsert<span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; conn = MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host = <span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #dc143c;">user</span> = <span style="color: #483d8b;">&quot;USER&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;passwd = <span style="color: #483d8b;">&quot;PASS&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;db = <span style="color: #483d8b;">&quot;DB&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor = conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Need to do this since AUTOCOMMIT = 0 by default (wtf?)</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SET AUTOCOMMIT=1&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>It may not be the cleanest method to open a new connection to MySQL for every worker subprocess.  Since MySQL opens a new thread for every connection, I don&#8217;t think this is that big of a deal.  However I am always open to new ways of doing things.</code></p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='Permanent Link: When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
<li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Permanent Link: Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/' rel='bookmark' title='Permanent Link: Database Read/Write Splitting in Frameworks/ORMs'>Database Read/Write Splitting in Frameworks/ORMs</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</title>
		<link>http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/</link>
		<comments>http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 13:00:36 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=480</guid>
		<description><![CDATA[While writing a simple Python script to access and process data in a database, I came across an error that said: Error 2014: Commands out of sync; you can't run this command now After quite a bit of Googling and with very little findings, I had to dive in a little and try to figure [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fpython%2Fpythons-mysqldb-2014-error-commands-out-of-sync%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fpython%2Fpythons-mysqldb-2014-error-commands-out-of-sync%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>While writing a simple Python script to access and process data in a database, I came across an error that said:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Error <span style="color: #ff4500;">2014</span>: Commands out of sync<span style="color: #66cc66;">;</span> you can<span style="color: #483d8b;">'t run this command now</span></div></div>
<p>After quite a bit of Googling and with very little findings, I had to dive in a little and try to figure out what was going on.  The whole error looked like this:<br />
<span id="more-480"></span></p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: black;">&#91;</span>elubow@web7 scripts<span style="color: black;">&#93;</span>$ ./<span style="color: #ff4500;">2014</span>_test.<span style="color: black;">py</span><br />
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:<br />
&nbsp; File <span style="color: #483d8b;">&quot;build/bdist.linux-x86_64/egg/MySQLdb/cursors.py&quot;</span>, line <span style="color: #ff4500;">173</span>, <span style="color: #ff7700;font-weight:bold;">in</span> execute<br />
&nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">errorhandler</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, exc, value<span style="color: black;">&#41;</span><br />
&nbsp; File <span style="color: #483d8b;">&quot;build/bdist.linux-x86_64/egg/MySQLdb/connections.py&quot;</span>, line <span style="color: #ff4500;">36</span>, <span style="color: #ff7700;font-weight:bold;">in</span> defaulterrorhandler<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> errorclass, errorvalue<br />
ProgrammingError: <span style="color: black;">&#40;</span><span style="color: #ff4500;">2014</span>, <span style="color: #483d8b;">&quot;Commands out of sync; you can't run this command now&quot;</span><span style="color: black;">&#41;</span><br />
Error <span style="color: #ff4500;">2014</span>: Commands out of sync<span style="color: #66cc66;">;</span> you can<span style="color: #483d8b;">'t run this command now<br />
Exception _mysql_exceptions.ProgrammingError: (1064, &quot;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 '</span>test_table<span style="color: #483d8b;">' at line 1&quot;) in<br />
&lt;bound method Cursor.__del__ of &lt;MySQLdb.cursors.Cursor object at 0x2aac002f9d90&gt;&gt; ignored<br />
&lt;/bound&gt;</span></div></div>
<p>The issue turned out to be a very simple one to fix.  Apparently MySQLdb doesn&#8217;t support compound MySQL statements.  That is to say that this won&#8217;t work:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SELECT COUNT(*) FROM people; SHOW TABLES LIKE addresses;'</span><span style="color: black;">&#41;</span></div></div>
<p>But this will work:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT COUNT(*) FROM people&quot;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor = conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SHOW TABLES LIKE addresses&quot;</span><span style="color: black;">&#41;</span></div></div>
<p>Make sure you break up your SQL statements into separate execute statements and you should avoid this MySQL 2014 error.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Permanent Link: Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='Permanent Link: When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/mysql-error-1033-incorrect-information-in-file/' rel='bookmark' title='Permanent Link: MySQL Error 1033: Incorrect Information in File'>MySQL Error 1033: Incorrect Information in File</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
