<?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>Calcifer &#187; mysql</title>
	<atom:link href="http://blog.calcifer.com.ar/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.calcifer.com.ar</link>
	<description></description>
	<lastBuildDate>Sat, 02 Oct 2010 05:32:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Accediendo a MySQL con Python y MySQLdb</title>
		<link>http://blog.calcifer.com.ar/2009/03/accediendo-a-mysql-con-python-y-mysqldb/</link>
		<comments>http://blog.calcifer.com.ar/2009/03/accediendo-a-mysql-con-python-y-mysqldb/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 15:00:27 +0000</pubDate>
		<dc:creator>lvidarte</dc:creator>
				<category><![CDATA[None]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.calcifer.com.ar/?p=330</guid>
		<description><![CDATA[MySQLdb es un módulo que implementa la API estándar (PEP249) para manejo de bases de datos, en este caso MySQL. MySQLdb es en realidad un wrapper del módulo _mysql que provee Python, el cual implementa la mayoría de las funciones definidas en la API C de MySQL. La idea de definir una API estándar es [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sourceforge.net/projects/mysql-python" class="external">MySQLdb</a> es un módulo que implementa la <a href="http://www.python.org/dev/peps/pep-0249/">API estándar (PEP249)</a> para manejo de bases de datos, en este caso <a href="http://www.mysql.com" class="external">MySQL</a>. MySQLdb es en realidad un wrapper del módulo _mysql que provee Python, el cual implementa la mayoría de las funciones definidas en la <a href="http://mysql-python.sourceforge.net/MySQLdb.html#mysql-c-api-function-mapping">API C de MySQL</a>. La idea de definir una API estándar es que uno debería poder cambiar a otra base de datos sin modificar demasiado el código.</p>
<blockquote title="python.org"><p>This API has been defined to encourage similarity between the Python modules that are used to access databases.</p></blockquote>
<h3>Instalación MySQLdb</h3>
<p>El paquete que nos provee el módulo se llama python-mysqldb. Podemos comprobar que lo tenemos instalado mediante</p>
<div class="codecolorer-container bash " style="overflow:auto;white-space:nowrap;width:435px"><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw2">dpkg</span> <span class="re5">--get-selections</span> <span class="sy0">|</span> <span class="kw2">grep</span> python-mysqldb</div></div>
<p>Si no existe lo instalamos</p>
<div class="codecolorer-container bash " style="overflow:auto;white-space:nowrap;width:435px"><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw2">sudo</span> <span class="kw2">apt-get</span> <span class="kw2">install</span> python-mysqldb</div></div>
<p>Para comprobar que efectivamente tenemos el módulo disponible y funcionando llamamos al <a href="http://docs.python.org/tutorial/interpreter.html" class="external">intérprete de Python</a> y escribimos</p>
<div class="codecolorer-container python " style="overflow:auto;white-space:nowrap;width:435px"><div class="python codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&gt;&gt;&gt;</span> <span class="kw1">import</span> MySQLdb</div></div>
<p>Un silencioso retorno de carro indica que podemos <a href="http://xkcd.com/353/" class="external">el módulo ha sido cargado</a>. Salimos del intérprete con<code>C-d</code>.</p>
<h3>Creación de una DB de pruebas</h3>
<p>Para nuestro ejemplo crearemos una base de datos con una única tabla. Entramos a la <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql.html" class="external">consola de MySQL</a> y escribimos</p>
<div class="codecolorer-container mysql " style="overflow:auto;white-space:nowrap;width:435px"><div class="mysql codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql<span class="sy1">&gt;</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=CREATE&amp;lr=lang_en"><span class="kw1">CREATE</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=DATABASE&amp;lr=lang_en"><span class="kw2">database</span></a> test<span class="sy2">;</span><br />
Query OK<span class="sy2">,</span> <span class="nu0">1</span> row affected <span class="br0">&#40;</span><span class="nu0">0.01</span> sec<span class="br0">&#41;</span><br />
<br />
mysql<span class="sy1">&gt;</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=USE&amp;lr=lang_en"><span class="kw1">USE</span></a> test<span class="sy2">;</span><br />
<a href="http://search.mysql.com/search?site=refman-51&amp;q=DATABASE&amp;lr=lang_en"><span class="kw2">Database</span></a> changed<br />
mysql<span class="sy1">&gt;</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=CREATE&amp;lr=lang_en"><span class="kw1">CREATE</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=TABLE&amp;lr=lang_en"><span class="kw1">TABLE</span></a> users <span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> id <a href="http://search.mysql.com/search?site=refman-51&amp;q=INT&amp;lr=lang_en"><span class="kw4">INT</span></a> <a href="http://dev.mysql.com/doc/refman/5.1/en/non-typed-operators.html"><span class="kw10">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=NULL&amp;lr=lang_en"><span class="kw3">NULL</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=AUTO_INCREMENT&amp;lr=lang_en"><span class="kw6">AUTO_INCREMENT</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=PRIMARY%20KEY&amp;lr=lang_en"><span class="kw1">PRIMARY KEY</span></a><span class="sy2">,</span><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> name <a href="http://search.mysql.com/search?site=refman-51&amp;q=VARCHAR&amp;lr=lang_en"><span class="kw4">VARCHAR</span></a><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span><span class="sy2">,</span><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> lastname <a href="http://search.mysql.com/search?site=refman-51&amp;q=VARCHAR&amp;lr=lang_en"><span class="kw4">VARCHAR</span></a><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> <span class="br0">&#41;</span><span class="sy2">;</span><br />
Query OK<span class="sy2">,</span> <span class="nu0">0</span> rows affected <span class="br0">&#40;</span><span class="nu0">0.01</span> sec<span class="br0">&#41;</span><br />
<br />
mysql<span class="sy1">&gt;</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=INSERT&amp;lr=lang_en"><span class="kw2">INSERT</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=INTO&amp;lr=lang_en"><span class="kw1">INTO</span></a> users <span class="br0">&#40;</span>name<span class="sy2">,</span> lastname<span class="br0">&#41;</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=VALUES&amp;lr=lang_en"><span class="kw1">VALUES</span></a><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> <span class="br0">&#40;</span><span class="st0">'Juan'</span><span class="sy2">,</span> <span class="st0">'Perez'</span><span class="br0">&#41;</span><span class="sy2">,</span><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> <span class="br0">&#40;</span><span class="st0">'Ana Maria'</span><span class="sy2">,</span> <span class="st0">'Lopez'</span><span class="br0">&#41;</span><span class="sy2">,</span><br />
&nbsp; &nbsp; <span class="sy1">-&gt;</span> <span class="br0">&#40;</span><span class="st0">'Carlos L.'</span><span class="sy2">,</span> <span class="st0">'Gutierrez'</span><span class="br0">&#41;</span><span class="sy2">;</span><br />
Query OK<span class="sy2">,</span> <span class="nu0">3</span> rows affected <span class="br0">&#40;</span><span class="nu0">0.00</span> sec<span class="br0">&#41;</span><br />
Records: <span class="nu0">3</span> &nbsp;Duplicates: <span class="nu0">0</span> &nbsp;<a href="http://search.mysql.com/search?site=refman-51&amp;q=WARNINGS&amp;lr=lang_en"><span class="kw1">Warnings</span></a>: <span class="nu0">0</span></div></div>
<p>Nuestra tabla users quedó entonces</p>
<div class="codecolorer-container mysql " style="overflow:auto;white-space:nowrap;width:435px"><div class="mysql codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql<span class="sy1">&gt;</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=SELECT&amp;lr=lang_en"><span class="kw1">SELECT</span></a> <span class="sy1">*</span> <a href="http://search.mysql.com/search?site=refman-51&amp;q=FROM&amp;lr=lang_en"><span class="kw1">FROM</span></a> users<span class="sy2">;</span><br />
<span class="sy1">+----+-----------+-----------+</span><br />
<span class="sy1">|</span> id <span class="sy1">|</span> name &nbsp; &nbsp; &nbsp;<span class="sy1">|</span> lastname &nbsp;<span class="sy1">|</span><br />
<span class="sy1">+----+-----------+-----------+</span><br />
<span class="sy1">|</span> &nbsp;<span class="nu0">1</span> <span class="sy1">|</span> Juan &nbsp; &nbsp; &nbsp;<span class="sy1">|</span> Perez &nbsp; &nbsp; <span class="sy1">|</span> <br />
<span class="sy1">|</span> &nbsp;<span class="nu0">2</span> <span class="sy1">|</span> Ana Maria <span class="sy1">|</span> Lopez &nbsp; &nbsp; <span class="sy1">|</span> <br />
<span class="sy1">|</span> &nbsp;<span class="nu0">3</span> <span class="sy1">|</span> Carlos L. <span class="sy1">|</span> Gutierrez <span class="sy1">|</span> <br />
<span class="sy1">+----+-----------+-----------+</span><br />
<span class="nu0">3</span> rows <a href="http://search.mysql.com/search?site=refman-51&amp;q=IN&amp;lr=lang_en"><span class="kw2">in</span></a> <a href="http://search.mysql.com/search?site=refman-51&amp;q=SET&amp;lr=lang_en"><span class="kw1">set</span></a> <span class="br0">&#40;</span><span class="nu0">0.00</span> sec<span class="br0">&#41;</span></div></div>
<h3>Creación del script mysqldb.py</h3>
<p>Creamos el siguiente script que se conectará a la base de datos test y traerá todos los datos de los usuarios cargados en la tabla users. En el script es necesario reemplazar &lt;user&gt; y &lt;pass&gt; por los datos requeridos para acceder a tu DB.</p>
<div class="codecolorer-container python " style="overflow:auto;white-space:nowrap;width:435px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="python codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co1">#!/usr/bin/python</span><br />
<span class="kw1">import</span> MySQLdb<br />
<br />
<span class="co1"># Conexion a la base de datos</span><br />
conn = MySQLdb.<span class="me1">connect</span> <span class="br0">&#40;</span>host=<span class="st0">&quot;localhost&quot;</span>,<br />
&nbsp; &nbsp; <span class="kw3">user</span>=<span class="st0">&quot;&lt;user&gt;&quot;</span>, passwd=<span class="st0">&quot;&lt;pass&gt;&quot;</span>, db=<span class="st0">&quot;test&quot;</span><span class="br0">&#41;</span><br />
<br />
<span class="co1"># Creacion cursor</span><br />
cursor = conn.<span class="me1">cursor</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<br />
<span class="co1"># Ejecucion query</span><br />
cursor.<span class="me1">execute</span><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM users&quot;</span><span class="br0">&#41;</span><br />
<br />
<span class="co1"># Manejo de datos devueltos por la consulta</span><br />
<span class="kw1">while</span> <span class="kw2">True</span>:<br />
&nbsp; &nbsp; row = cursor.<span class="me1">fetchone</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> row <span class="kw1">is</span> <span class="kw2">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span><br />
&nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;id: %s<span class="es0">\t</span>name: %s %s&quot;</span> <span class="sy0">%</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>row<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, row<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, row<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<br />
<span class="co1"># Finalizado cursor y objeto conexion</span><br />
cursor.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div></td></tr></tbody></table></div>
<p>La salida debería ser</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">id: 1 &nbsp; name: Juan Perez<br />
id: 2 &nbsp; name: Ana Maria Lopez<br />
id: 3 &nbsp; name: Carlos L. Gutierrez</div></div>
<h4>Links recomendados para ampliar conocimientos</h4>
<ul>
<li><a href="http://mysql-python.sourceforge.net/MySQLdb.html" class="external">MySQLdb User&#8217;s Guide</a></li>
<li><a href="http://www.kitebird.com/articles/pydbapi.html" class="external">Writing MySQL Scripts with Python DB-API</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.calcifer.com.ar/2009/03/accediendo-a-mysql-con-python-y-mysqldb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Expresiones regulares en MySQL</title>
		<link>http://blog.calcifer.com.ar/2006/09/expresiones-regulares-en-mysql/</link>
		<comments>http://blog.calcifer.com.ar/2006/09/expresiones-regulares-en-mysql/#comments</comments>
		<pubDate>Sat, 30 Sep 2006 16:10:58 +0000</pubDate>
		<dc:creator>lvidarte</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://blog.calcifer.com.ar/2006/09/30/expresiones-regulares-en-mysql/</guid>
		<description><![CDATA[En MySQL pueden usarse expresiones regulares mediante el operador
REGEXP
:
^
Comienzo de una cadena.

mysql&#62; SELECT 'fo\nfo' REGEXP '^fo$'; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; -&#62; 0
mysql&#62; SELECT 'fofo' REGEXP '^fo'; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;-&#62; 1

$
Final de una cadena.

mysql&#62; SELECT 'fo\no' REGEXP '^fo\no$'; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>En MySQL pueden usarse expresiones regulares mediante el operador</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">REGEXP</div></div>
<p>:</p>
<h3 class="margin20 pink">^</h3>
<p class="margin5">Comienzo de una cadena.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'fo\nfo' REGEXP '^fo$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0<br />
mysql&gt; SELECT 'fofo' REGEXP '^fo'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1</div></div>
</pre>
<h3 class="margin20 pink">$</h3>
<p class="margin5">Final de una cadena.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'fo\no' REGEXP '^fo\no$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'fo\no' REGEXP '^fo$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0</div></div>
</pre>
<h3 class="pink margin20">.</h3>
<p class="margin5">Cualquier caracter (incluído <a href="/2006/08/18/caracteres-de-salto-de-linea/" title="Caracteres de salto de línea" target="_blank">line feed y carriage return</a>).</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'fofo' REGEXP '^f.*$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'fo\r\nfo' REGEXP '^f.*$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1</div></div>
</pre>
<p><span id="more-57"></span></p>
<h3 class="pink margin20">a*</h3>
<p class="margin5">Secuencia de cero o más caracteres.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'Ban' REGEXP '^Ba*n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'Baaan' REGEXP '^Ba*n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'Bn' REGEXP '^Ba*n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1</div></div>
</pre>
<h3 class="pink margin20">a+</h3>
<p class="margin5">Secuencia de uno o más caracteres.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'Ban' REGEXP '^Ba+n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'Bn' REGEXP '^Ba+n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0</div></div>
</pre>
<h3 class="pink margin20">a?</h3>
<p class="margin5">Cero o un caracter.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'Bn' REGEXP '^Ba?n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'Ban' REGEXP '^Ba?n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'Baan' REGEXP '^Ba?n'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0</div></div>
</pre>
<h3 class="pink margin20">de|abc</h3>
<p class="margin5">Operador lógico OR. Cualquiera de las secuencias &#8216;de&#8217; o &#8216;abc&#8217;.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'pi' REGEXP 'pi|apa'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'axe' REGEXP 'pi|apa'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0<br />
mysql&gt; SELECT 'apa' REGEXP 'pi|apa'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'apa' REGEXP '^(pi|apa)$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'pi' REGEXP '^(pi|apa)$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'pix' REGEXP '^(pi|apa)$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0</div></div>
</pre>
<h3 class="pink margin20">(abc)*</h3>
<p class="margin5">Match de una o más instancias de la secuencia &#8216;abc&#8217;.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'pi' REGEXP '^(pi)*$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'pip' REGEXP '^(pi)*$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0<br />
mysql&gt; SELECT 'pipi' REGEXP '^(pi)*$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1</div></div>
</pre>
<h3 class="pink margin20">{1}, {2,3}</h3>
<p class="margin5">La notación</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">{n}</div></div>
<p>o</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">{m,n}</div></div>
<p>provee una forma más general en la cual hacer match de varias ocurrencias del pattern que precede, donde m indica cantidad mínima de ocurrencias y n indica máxima. Ambos valores son enteros.</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a*</div></div>
<p>Puede escribirse como</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a{0,}</div></div>
<p>.</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a+</div></div>
<p>Puede escribirse como</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a{1,}</div></div>
<p>.</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a?</div></div>
<p>Puede escribirse como</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a{0,1}</div></div>
<p>.</p>
<p>Para ser más precisos,</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a{n}</div></div>
<p>hace match de, exactamente, n instancias de a.</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a{n,}</div></div>
<p>hace match de n o más instancias de a.</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">a{m,n}</div></div>
<p>hace match de entre m y n instancias de a, donde m y n están incluídos.</p>
<p>m y n deben estar en el rango desde 0 a</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">RE_DUP_MAX</div></div>
<p>(default 255), inclusive. Si se especifican ambos valores (m y n), m debe ser menor o igual a n.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'abcde' REGEXP 'a[bcd]{2}e'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0<br />
mysql&gt; SELECT 'abcde' REGEXP 'a[bcd]{3}e'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'abcde' REGEXP 'a[bcd]{1,10}e'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1</div></div>
</pre>
<h3 class="pink margin20">[a-dX], [^a-dX]</h3>
<p class="margin5">Match de cualquier caracter perteneciente al rango a-dX (a, b, c, d, X) y en el caso que el rango sea precedido por</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">^</div></div>
<p>equivale a cualquier caracter que no corresponda con dicho rango. Por ejemplo,</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">[0-9]</div></div>
<p>hace match de cualquier dígito decimal, mientras que</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">[^0-9]</div></div>
<p>de cualquier caracter no decimal.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'aXbc' REGEXP '[a-dXYZ]'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'aXbc' REGEXP '^[a-dXYZ]$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0<br />
mysql&gt; SELECT 'aXbc' REGEXP '^[a-dXYZ]+$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'aXbc' REGEXP '^[^a-dXYZ]+$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0<br />
mysql&gt; SELECT 'gheis' REGEXP '^[^a-dXYZ]+$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 1<br />
mysql&gt; SELECT 'gheisa' REGEXP '^[^a-dXYZ]+$'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0</div></div>
</pre>
<h3 class="pink margin20">[.characters.]</h3>
<p class="margin5">Esta expresión permite hacer match de un caracter mediante su nombre, como por ejemplo</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">[.newline.]</div></div>
<p>en lugar de \n. La lista completa puede encontrarse en regexp/cname.h file.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT '~' REGEXP '[[.~.]]'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT '~' REGEXP '[[.tilde.]]'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1</div></div>
</pre>
<h3 class="pink margin20">[:character_class:]</h3>
<p class="margin5">MySQL, como tantos otros lenguajes que implementan expresiones regulares, incluye una serie de alias para denominar clases de caracteres. La siguiente tabla es una lista de las clases y sus nombres.</p>
<table class="table1">
<tr>
<td>alnum</td>
<td>Alphanumeric characters</td>
</tr>
<tr>
<td>alpha</td>
<td>Alphabetic characters</td>
</tr>
<tr>
<td>blank</td>
<td>Whitespace characters</td>
</tr>
<tr>
<td>cntrl</td>
<td>Control characters</td>
</tr>
<tr>
<td>digit</td>
<td>Digit characters</td>
</tr>
<tr>
<td>graph</td>
<td>Graphic characters</td>
</tr>
<tr>
<td>lower</td>
<td>Lowercase alphabetic characters</td>
</tr>
<tr>
<td>print</td>
<td>Graphic or space characters</td>
</tr>
<tr>
<td>punct</td>
<td>Punctuation characters</td>
</tr>
<tr>
<td>space</td>
<td>Space, tab, newline, and carriage return</td>
</tr>
<tr>
<td>upper</td>
<td>Uppercase alphabetic characters</td>
</tr>
<tr>
<td>xdigit</td>
<td>Hexadecimal digit characters</td>
</tr>
</table>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'justalnums' REGEXP '[[:alnum:]]+'; &nbsp; &nbsp; &nbsp; -&gt; 1<br />
mysql&gt; SELECT '!!' REGEXP '[[:alnum:]]+'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0</div></div>
</pre>
<h3 class="pink margin20">[[:<:]], [[:>:]]</h3>
<p class="margin5">Estos marcadores son usados para marcar límites de palabras. Ellos hacen match del comienzo y final de una palabra, respectivamente. Una palabra es una secuencia de caracteres pertenecientes a la clase</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">alnum</div></div>
<p>(alphanumeric) y al caracter underscore (_). La definición de palabra dice también que dicha secuencia no debe ser precedida o seguida de ningún caracter de los mencionados.</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT 'a word a' REGEXP '[[:&lt;:]]word[[:&gt;:]]'; &nbsp; -&gt; 1<br />
mysql&gt; SELECT 'a xword a' REGEXP '[[:&lt;:]]word[[:&gt;:]]'; &nbsp;-&gt; 0</div></div>
</pre>
<h3 class="pink margin20">\</h3>
<p class="margin5">Para hacer que los caracteres especiales pierdan su significado deben ser precedidos por dos backslash (\). El analizador de MySQL interpreta uno de los \ y la librería de expresiones regulares el otro. Por ejemplo, para hacer un match de la cadena &#8216;1+2&#8242; la cual contiene el caracter especial +, sólo la última expresión regular es correcta:</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT '1+2' REGEXP '1+2'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 0<br />
mysql&gt; SELECT '1+2' REGEXP '1\+2'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt; 0<br />
mysql&gt; SELECT '1+2' REGEXP '1\\+2'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; 1</div></div>
</pre>
<h3 class="pink margin20">=0, =1</h3>
<p class="margin5">Invertir el match de una regex es tan simple como comparar el resultado con 1 o 0. En el ejemplo se muestra primero el resultado de una consulta simple, luego sólo los registros que contengan el caracter &#8216;5&#8242; y finalmente se invierte el filtro. También funciona con true y false</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&amp;gt; SELECT link_url FROM wp_links;<br />
+------------------------------+<br />
| link_url &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+------------------------------+<br />
| http://barrapunto.com &nbsp; &nbsp; &nbsp; &nbsp;|<br />
| http://www.microsiervos.com/ |<br />
| http://www.kriptopolis.org/ &nbsp;|<br />
| http://meneame.net/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
| http://www.error500.net/ &nbsp; &nbsp; |<br />
| http://slashdot.org &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
| http://astroseti.org/ &nbsp; &nbsp; &nbsp; &nbsp;|<br />
+------------------------------+</div></div>
</pre>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&amp;gt; SELECT link_url FROM wp_links WHERE link_url REGEXP '5';<br />
+--------------------------+<br />
| link_url &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+--------------------------+<br />
| http://www.error500.net/ |<br />
+--------------------------+</div></div>
</pre>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&amp;gt; SELECT link_url FROM wp_links WHERE link_url REGEXP '5'=0;<br />
+------------------------------+<br />
| link_url &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+------------------------------+<br />
| http://barrapunto.com &nbsp; &nbsp; &nbsp; &nbsp;|<br />
| http://www.microsiervos.com/ |<br />
| http://www.kriptopolis.org/ &nbsp;|<br />
| http://meneame.net/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
| http://slashdot.org &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
| http://astroseti.org/ &nbsp; &nbsp; &nbsp; &nbsp;|<br />
+------------------------------+</div></div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.calcifer.com.ar/2006/09/expresiones-regulares-en-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Client does not support authentication protocol</title>
		<link>http://blog.calcifer.com.ar/2006/08/client-does-not-support-authentication-protocol/</link>
		<comments>http://blog.calcifer.com.ar/2006/08/client-does-not-support-authentication-protocol/#comments</comments>
		<pubDate>Wed, 30 Aug 2006 20:02:41 +0000</pubDate>
		<dc:creator>lvidarte</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.calcifer.com.ar/2006/08/30/client-does-not-support-authentication-protocol/</guid>
		<description><![CDATA[Luego de agregar un nuevo usuario MySQL he tenido el siguiente problema al intentar conectarme a la DB usando PHP4:

Client does not support authentication protocol requested by server;
consider upgrading MySQL client

Buscando llego a: http://dev.mysql.com/doc/refman/5.0/en/password-hashing.html
El problema
The password hashing mechanism was updated in MySQL 4.1 to provide better security and to reduce the risk of passwords being [...]]]></description>
			<content:encoded><![CDATA[<p>Luego de agregar un nuevo usuario MySQL he tenido el siguiente problema al intentar conectarme a la DB usando PHP4:</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">Client does not support authentication protocol requested by server;<br />
consider upgrading MySQL client</div></div>
</pre>
<p>Buscando llego a: <a href="http://dev.mysql.com/doc/refman/5.0/en/password-hashing.html" class="linkexternal">http://dev.mysql.com/doc/refman/5.0/en/password-hashing.html</a></p>
<h3>El problema</h3>
<blockquote><p>The password hashing mechanism was updated in MySQL 4.1 to provide better security and to reduce the risk of passwords being intercepted. However, this new mechanism is understood only by MySQL 4.1 (and newer) servers and clients, which can result in some compatibility problems.</p></blockquote>
<p>Hace un tiempo pasé de la versión 4 a la 4.1. Resulta que ahora el hash correspondiente a las passwords se realiza utilizando un nuevo algoritmo que genera un hash de 41 bytes frente al viejo de 16 bytes.</p>
<p>Antes de la versión 4.1 la función</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">PASSWORD()</div></div>
<p>devolvía un hash de 16 bytes:</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT PASSWORD('mypass');<br />
+--------------------+<br />
| PASSWORD('mypass') |<br />
+--------------------+<br />
| 6f8c114b58f2ce9e &nbsp; |<br />
+--------------------+</div></div>
</pre>
<p>A partir de la versión 4.1 la función</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">PASSWORD()</div></div>
<p>devuelve un hash de 41 bytes:</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SELECT PASSWORD('mypass');<br />
+-------------------------------------------+<br />
| PASSWORD('mypass') &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
+-------------------------------------------+<br />
| *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4 |<br />
+-------------------------------------------+</div></div>
</pre>
<h3>Solución</h3>
<p>Según: <a href="http://dev.mysql.com/doc/refman/5.0/en/php-problems.html" class="linkexternal">Common Problems with MySQL and PHP</a>.</p>
<blockquote><p><strong>Error: Client does not support authentication protocol</strong>: This is most often encountered when trying to use the older mysql extension with MySQL 4.1.1 and later. Possible solutions are: downgrade to MySQL 4.0; switch to PHP 5 and the newer mysqli extension; or configure the MySQL server with &#8211;old-passwords. (See Section A.2.3, “Client does not support authentication protocol”, for more information.)</p></blockquote>
<h3>Otra solución</h3>
<p>La función</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">OLD_PASSWORD</div></div>
<p>devuelve el viejo hash de 16 bytes:</p>
<pre>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:435px"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">mysql&gt; SET PASSWORD FOR 'some_user'@'some_host' = OLD_PASSWORD('newpwd');</div></div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.calcifer.com.ar/2006/08/client-does-not-support-authentication-protocol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

