<?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>Dave Rowe's Blog &#187; Grinds My Gears</title>
	<atom:link href="http://blog.roweware.com/category/grinds-my-gears/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.roweware.com</link>
	<description>Ramblings about things I think I know...</description>
	<lastBuildDate>Wed, 14 Jul 2010 17:45:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>&#8216;Correct&#8217; SQL INSERT Syntax</title>
		<link>http://blog.roweware.com/2009/02/05/correct-sql-insert-syntax/</link>
		<comments>http://blog.roweware.com/2009/02/05/correct-sql-insert-syntax/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 15:23:22 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Grinds My Gears]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=62</guid>
		<description><![CDATA[Recently I encountered an issue at work.  I had recently merged some code, and someone who tested from start to finish said there was an error right off the bat.  Crap. Looking at the error, my blood began to boil: ?View Code DOSPHP Fatal error: Column count doesn't match value count at row 1 So, [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I encountered an issue at work.  I had recently merged some code, and someone who tested from start to finish said there was an error right off the bat.  Crap.</p>
<p>Looking at the error, my blood began to boil<code>:</code></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p62code4'); return false;">View Code</a> DOS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p624"><td class="code" id="p62code4"><pre class="dos" style="font-family:monospace;">PHP Fatal error: Column count doesn't match value count at row <span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<p>So, I investigated where this was happening, and found it.  Effectively, when you write an INSERT like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p62code5'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p625"><td class="code" id="p62code5"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #993333; font-weight: bold;">table</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>The DB will automatically match your values up with the columns.  Except, you&#8217;ve effectively broken the code because any change to the schema will change the number of columns.  Second, you cannot guarantee the column order on a table, so you just broke any chance of compatibility with another DB back-end.  Especially, in the case of multiple branches of a project being updated, you <em>must</em> be defensive about coding practices against the database.  You cannot assume anything about the mainline of development beyond your branch.</p>
<p>So, a small lesson.  Anytime you&#8217;re writing an INSERT, always, always, always, declare the column listing like so:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p62code6'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p626"><td class="code" id="p62code6"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #993333; font-weight: bold;">table</span> <span style="color: #66cc66;">&#40;</span>col_1<span style="color: #66cc66;">,</span> col_2<span style="color: #66cc66;">,</span> col_3<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Now, you see that the DB doesn&#8217;t have to think about which columns you&#8217;re populating, you told it directly.  This also guards against the change in column order, as well as, the addition of new columns.  Remember, if you&#8217;re adding columns that are set to &#8216;NOT NULL&#8217;, be sure to set a sane acceptable default, and you shouldn&#8217;t have compatibility issues with your old inserts.</p>
<p>Closing side benefit, I can look at this new INSERT, and know what the columns are.  Given the first, I&#8217;d have to go check the DB, and verify the column order, etc.  Always put column names in.  Always.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/02/05/correct-sql-insert-syntax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Grinds My Gears &#8211; 60 Minutes Fat Report</title>
		<link>http://blog.roweware.com/2007/11/19/grinds-my-gears-60-minutes-fat-report/</link>
		<comments>http://blog.roweware.com/2007/11/19/grinds-my-gears-60-minutes-fat-report/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 15:13:04 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Grinds My Gears]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/2007/11/19/grinds-my-gears-60-minutes-fat-report/</guid>
		<description><![CDATA[Peter Griffin of Family Guy used to work at the news station in Quahog, writing a piece called &#8220;What really grinds my gears&#8221;, I pay homage to that style with a &#8220;Grinds My Gears&#8221; post. Today on the radio one of the co-hosts was talking about a report from 60 Minutes about requiring restaurants in [...]]]></description>
			<content:encoded><![CDATA[<p>Peter Griffin of <a href="http://www.familyguy.com" target="_blank">Family Guy</a> used to work at the news station in Quahog, writing a piece called &#8220;What really grinds my gears&#8221;, I pay homage to that style with a &#8220;Grinds My Gears&#8221; post.</p>
<p>Today on the <a href="http://www.channel941.com" target="_blank">radio</a> one of the co-hosts was talking about a <a href="http://www.cbsnews.com/stories/2007/11/16/60minutes/main3513549.shtml" target="_blank">report</a> from 60 Minutes about requiring restaurants in New York City to post nutritional information on the menus.  As if the nutritional board wasn&#8217;t enough, or if you really cared, asking the manager somehow failed to get you the information you&#8217;ve been seeking.</p>
<p>You know &#8220;what really grinds my gears&#8221;?  People blaming the fast food industry for making America fatter.  As if people that eat fast food don&#8217;t realize that the food isn&#8217;t good for them.  Ask anyone on the street, &#8220;If I dunk food in a vat of boiling oil (fat), is it healthy for me?&#8221;  Of course not!  People know it, but they keep eating it.  What reason does McDonalds have to change the way it makes food?  McDonalds doesn&#8217;t have the responsibility to maintain the health of America.  Secondly, if McDonalds doesn&#8217;t provide the fat, someone else will.  McDonalds isn&#8217;t the problem here, folks.  It&#8217;s the people visiting the McDonalds.</p>
<p>Now if the health industry wants to run some type of campaign to make America healthier, and they use stats from the fast food companies for nutritional content (or lack thereof), by all means.  But, in no way should they create legislation that affects the fast food industries business (by directly making them change anything).  I believe anything they want to do to change the business should be indirect.  Make the <em>people</em> want to change.  If the people want McDonalds to change, it will.  Until then, nothing will change.</p>
<p>Hopefully, this isn&#8217;t the last &#8220;Grinds My Gears&#8221; post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2007/11/19/grinds-my-gears-60-minutes-fat-report/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
