<?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>SIMOTRONE WEB PAGE &#187; Mysql</title>
	<atom:link href="http://www.simotrone.it/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simotrone.it</link>
	<description>Simotrone is back!</description>
	<lastBuildDate>Fri, 30 Jul 2010 05:47:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL e timestamp, la vendetta</title>
		<link>http://www.simotrone.it/2010/03/02/mysql-e-timestamp-la-vendetta/</link>
		<comments>http://www.simotrone.it/2010/03/02/mysql-e-timestamp-la-vendetta/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 06:25:24 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=2691</guid>
		<description><![CDATA[Dopo gli accidenti tirati precedentemente, ho scoperto che dare un comando CHANGE su una colonna dichiarandola come TIMESTAMP ha il seguente effetto: il default diventa CURRENT_TIMESTAMP e l&#8217;extra è ON UPDATE CURRENT_TIMESTAMP.
Avevo letto che CHANGE e MODIFY cancellavano gli attributi (l&#8217;Extra) delle colonne cui cambiavano il tipo, e così mi sono messo ad assegnare TIMESTAMP [...]]]></description>
			<content:encoded><![CDATA[<p>Dopo gli accidenti tirati precedentemente, ho scoperto che dare un comando CHANGE su una colonna dichiarandola come TIMESTAMP ha il seguente effetto: il default diventa CURRENT_TIMESTAMP e l&#8217;extra è ON UPDATE CURRENT_TIMESTAMP.</p>
<p>Avevo letto che CHANGE e MODIFY cancellavano gli attributi (<em>l&#8217;Extra</em>) delle colonne cui cambiavano il tipo, e così mi sono messo ad assegnare TIMESTAMP e DATETIME ai due campi created/updated in modo da togliermi i dubbi.<br />
(dal man: &#8220;<em>When you use CHANGE or MODIFY, column_definition must include the data type and all attributes that should apply to the new column, other than index attributes such as PRIMARY KEY or UNIQUE. Attributes present in the original definition but not specified for the new definition are not carried forward.</em>&#8220;)</p>
<pre>sim:test> ALTER TABLE A CHANGE created created DATETIME;
sim:test> ALTER TABLE A CHANGE updated updated TIMESTAMP;
[...]
| created | datetime    | YES  | | NULL              |                             |
| updated | timestamp   | NO   | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |</pre>
<p>Alla fine mi sono un po&#8217; capito leggendo <a href="http://dev.mysql.com/doc/refman/5.1/en/timestamp.html">qua</a>.<br />
I dati di tipo <strong>timestamp</strong> dovrebbero essere dei <strong>datetime</strong> (e lo sono come formato), ma a differenza di questi ultimi si aggiornano con l&#8217;ora attuale.</p>
<p><em>The TIMESTAMP data type offers automatic initialization and updating.</em></p>
<p>Dopo aver reso la colonna updated CURRENT_TIMESTAMP con aggiornamento sull&#8217;UPDATE dei dati, ho potuto fare:</p>
<pre>ALTER TABLE A MODIFY created TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';</pre>
<p>e inserire un paio di dati:</p>
<pre>sim:test> INSERT INTO A (word) VALUES ('gastone');
sim:test> INSERT INTO A (word,created) VALUES ('paperino',NULL);</pre>
<p>col risultato:</p>
<pre>|  5 | gastone  | 0000-00-00 00:00:00 | 2010-02-11 23:42:03 |
|  6 | paperino | 2010-02-11 23:42:23 | 2010-02-11 23:42:23 |</pre>
<p>Sul man c&#8217;è scritto pure questo, ma a me continua a sembrare un comportamento strano.<br />
<em>TIMESTAMP columns are NOT NULL by default, cannot contain NULL values, and assigning NULL assigns the current timestamp.</em></p>
<p>In conclusione, se volete avere dei campi con la creazione e l&#8217;aggiornamento automatico delle date incluso, leggetevi la <a href="http://dev.mysql.com/doc/refman/5.1/en/timestamp.html">pagina di Mysql sui timestamp</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2010/03/02/mysql-e-timestamp-la-vendetta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL e timestamp vanno sulla luna</title>
		<link>http://www.simotrone.it/2010/02/23/mysql-e-timestamp-vanno-sulla-luna/</link>
		<comments>http://www.simotrone.it/2010/02/23/mysql-e-timestamp-vanno-sulla-luna/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 06:10:55 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=2677</guid>
		<description><![CDATA[Qua c&#8217;è da uscire di senno. A me MySQL sta pure simpatico, ma fa veramente di tutto per farmi sentire male.
Parto proprio dall&#8217;inizio.
sim:none> CREATE DATABASE test CHARACTER SET = 'UTF8';
Query OK, 1 row affected (0.00 sec)

sim:none> CONNECT test;
Connection id:    230
Current database: test
Ammettiamo che io voglia creare una tabella canonica con vari campi, [...]]]></description>
			<content:encoded><![CDATA[<p>Qua c&#8217;è da uscire di senno. A me MySQL sta pure simpatico, ma fa veramente di tutto per farmi sentire male.</p>
<p>Parto proprio dall&#8217;inizio.</p>
<pre>sim:none> CREATE DATABASE test CHARACTER SET = 'UTF8';
Query OK, 1 row affected (0.00 sec)

sim:none> CONNECT test;
Connection id:    230
Current database: test</pre>
<p>Ammettiamo che io voglia creare una tabella canonica con vari campi, fra cui l&#8217;<strong>id</strong> come PK, un campo per segnare quando il record è stato creato (<strong>created</strong>) ed uno per segnarmi quando è stato aggiornato l&#8217;ultima volta (<strong>updated</strong>).</p>
<pre>sim:test> CREATE TABLE A (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    word VARCHAR(8) NOT NULL,
    created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated TIMESTAMP NOT NULL );
Query OK, 0 rows affected (0.00 sec)</pre>
<p>La situazione della mia tabella A per ora è chiara.</p>
<pre>sim:test> SHOW CREATE TABLE A \G;
*************************** 1. row ***************************
       Table: A
Create Table: CREATE TABLE `A` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `word` varchar(8) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
</pre>
<p>Inserisco anche un paio di record che appaiono come ci si aspetta:</p>
<pre>sim:test> INSERT INTO A (word) VALUES ('pippo'),('pluto');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

sim:test> SELECT * FROM A;
+----+-------+---------------------+---------------------+
| id | word  | created             | updated             |
+----+-------+---------------------+---------------------+
|  1 | pippo | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
|  2 | pluto | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
+----+-------+---------------------+---------------------+
2 rows in set (0.00 sec)
</pre>
<p>Nel caso si decida di aggiornare un record:</p>
<pre>sim:test> UPDATE A SET word = 'topolino' WHERE id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

sim:test> SELECT * FROM A;
+----+----------+---------------------+---------------------+
| id | word     | created             | updated             |
+----+----------+---------------------+---------------------+
|  1 | pippo    | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
|  2 | topolino | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
+----+----------+---------------------+---------------------+
2 rows in set (0.00 sec)</pre>
<p>A questo punto decido che questa disposizione dei dati non mi va più bene, che fare? <em>Semplice</em> (* le ultime parole famose <sup>TM</sup> ), leggo <a href="http://dev.mysql.com/doc/refman/5.1/en/alter-table.html">il manuale</a> e sistemo. Seee&#8230;</p>
<p>Un po&#8217; di pazzia MySQL:</p>
<pre>sim:test> SHOW COLUMNS FROM A;
+---------+------------------+------+-----+---------------------+----------------+
| Field   | Type             | Null | Key | Default             | Extra          |
+---------+------------------+------+-----+---------------------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| word    | varchar(8)       | NO   |     | NULL                |                |
| created | timestamp        | NO   |     | CURRENT_TIMESTAMP   |                |
| updated | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+---------+------------------+------+-----+---------------------+----------------+
4 rows in set (0.00 sec)

sim:test> ALTER TABLE A ALTER created DROP DEFAULT;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

sim:test> SHOW COLUMNS FROM A;
+---------+------------------+------+-----+---------------------+----------------+
| Field   | Type             | Null | Key | Default             | Extra          |
+---------+------------------+------+-----+---------------------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| word    | varchar(8)       | NO   |     | NULL                |                |
| created | timestamp        | NO   |     | NULL                |                |
| updated | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+---------+------------------+------+-----+---------------------+----------------+
4 rows in set (0.00 sec)

sim:test> ALTER TABLE A CHANGE created creat TIMESTAMP;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

sim:test> SHOW COLUMNS FROM A;
+---------+------------------+------+-----+---------------------+-----------------------------+
| Field   | Type             | Null | Key | Default             | Extra                       |
+---------+------------------+------+-----+---------------------+-----------------------------+
| id      | int(10) unsigned | NO   | PRI | NULL                | auto_increment              |
| word    | varchar(8)       | NO   |     | NULL                |                             |
| creat   | timestamp        | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| updated | timestamp        | NO   |     | 0000-00-00 00:00:00 |                             |
+---------+------------------+------+-----+---------------------+-----------------------------+
4 rows in set (0.00 sec)</pre>
<p>Con un magico DROP del DEFAULT di un campo ed un cambio nome ottengo di nuovo il CURRENT_TIMESTAMP come default, e pure l&#8217;update automatico sul campo.</p>
<p>Provare per credere:</p>
<pre>sim:test> INSERT INTO A (word) VALUES ('minnie');
Query OK, 1 row affected (0.00 sec)

sim:test> SELECT * FROM A;
+----+----------+---------------------+---------------------+
| id | word     | creat               | updated             |
+----+----------+---------------------+---------------------+
|  1 | pippo    | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
|  2 | topolino | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
|  3 | minnie   | 2010-02-11 23:02:36 | 0000-00-00 00:00:00 |
+----+----------+---------------------+---------------------+
3 rows in set (0.00 sec)

sim:test> UPDATE A  SET word = 'clarabella' WHERE id=3;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

sim:test> SELECT * FROM A;
+----+----------+---------------------+---------------------+
| id | word     | creat               | updated             |
+----+----------+---------------------+---------------------+
|  1 | pippo    | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
|  2 | topolino | 2010-02-11 22:23:41 | 0000-00-00 00:00:00 |
|  3 | clarabel | 2010-02-11 23:03:35 | 0000-00-00 00:00:00 |
+----+----------+---------------------+---------------------+
3 rows in set (0.00 sec)</pre>
<p>Robe da pazzi.</p>
<hr/>
<strong>Note:</strong><br />
1) MySQL aggiunge alle funzionalità dell&#8217;SQL (che vorrebbe una sola query per riga) la possibilità di comandi multipli in un solo <em>statement</em>:  ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2010/02/23/mysql-e-timestamp-vanno-sulla-luna/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conversione rapida</title>
		<link>http://www.simotrone.it/2010/02/16/conversione-rapida/</link>
		<comments>http://www.simotrone.it/2010/02/16/conversione-rapida/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 06:56:04 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[conversione]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[tabelle]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=2675</guid>
		<description><![CDATA[Dovevo trasferire rapidamente i dati da una singola tabella in un db MySQL a quattro distinte &#8211; in modo da normalizzare un po&#8217; i dati.
Con perl e DBI la cosa viene facile.

#!/usr/bin/perl
use strict;
use DBI;
&#91;&#8230;&#93;
my $dsn = &#34;dbi:mysql:database=$db_name&#34;;
my $dbh = DBI-&#62;connect&#40; $dsn, $username, $password &#41; or die $DBI::errstr;
my $sth = $dbh-&#62;prepare&#40;
&#160; &#160; &#8216;SELECT name, surname, birth, [...]]]></description>
			<content:encoded><![CDATA[<p>Dovevo trasferire rapidamente i dati da una singola tabella in un db MySQL a quattro distinte &#8211; in modo da <a href="http://en.wikipedia.org/wiki/Database_normalization">normalizzare</a> un po&#8217; i dati.</p>
<p>Con perl e DBI la cosa viene facile.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="co1">#!/usr/bin/perl</span></p>
<p><span class="kw2">use</span> strict;<br />
<span class="kw2">use</span> DBI;<br />
<span class="br0">&#91;</span>&#8230;<span class="br0">&#93;</span><br />
<span class="kw1">my</span> <span class="re0">$dsn</span> = <span class="st0">&quot;dbi:mysql:database=$db_name&quot;</span>;<br />
<span class="kw1">my</span> <span class="re0">$dbh</span> = DBI-&gt;<span class="me1">connect</span><span class="br0">&#40;</span> <span class="re0">$dsn</span>, <span class="re0">$username</span>, <span class="re0">$password</span> <span class="br0">&#41;</span> <span class="kw1">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span class="kw3">die</span></a> <span class="re0">$DBI</span>::<span class="me2">errstr</span>;</p>
<p><span class="kw1">my</span> <span class="re0">$sth</span> = <span class="re0">$dbh</span>-&gt;<span class="me1">prepare</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;SELECT name, surname, birth, address, cap, city, country, phone, email, notes FROM rubrica&#8217;</span><br />
<span class="br0">&#41;</span> <span class="kw1">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span class="kw3">die</span></a> <span class="re0">$dbh</span>-&gt;<span class="me1">errstr</span>;<br />
<span class="re0">$sth</span>-&gt;<span class="me1">execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">my</span> <span class="re0">$ins0</span> = <span class="re0">$dbh</span>-&gt;<span class="me1">prepare</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;INSERT INTO persone (nome,cognome) VALUES (?,?)&#8217;</span><br />
<span class="br0">&#41;</span> <span class="kw1">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span class="kw3">die</span></a> <span class="re0">$dbh</span>-&gt;<span class="me1">errstr</span>;<br />
<span class="kw1">my</span> <span class="re0">$ins1</span> = <span class="re0">$dbh</span>-&gt;<span class="me1">prepare</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; <span class="st0">&#8216;INSERT INTO indirizzi (id,via,cap,citta,paese) VALUES (?,?,?,?,?)&#8217;</span><br />
<span class="br0">&#41;</span> <span class="kw1">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span class="kw3">die</span></a> <span class="re0">$dbh</span>-&gt;<span class="me1">errstr</span>;<br />
<span class="br0">&#91;</span>&#8230; ecc, seguono altri statement insert &#8230;<span class="br0">&#93;</span></p>
<p><span class="kw1">while</span> <span class="br0">&#40;</span> <span class="kw1">my</span> <span class="re0">$row</span> = <span class="re0">$sth</span>-&gt;<span class="me1">fetchrow_hashref</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$ins0</span>-&gt;<span class="me1">execute</span><span class="br0">&#40;</span> <span class="re0">$row</span>-&gt;<span class="br0">&#123;</span>name<span class="br0">&#125;</span>, <span class="re0">$row</span>-&gt;<span class="br0">&#123;</span>surname<span class="br0">&#125;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">my</span> <span class="re0">$rv</span> = <span class="re0">$dbh</span>-&gt;<span class="me1">last_insert_id</span><span class="br0">&#40;</span><a href="http://perldoc.perl.org/functions/undef.html"><span class="kw3">undef</span></a>, <a href="http://perldoc.perl.org/functions/undef.html"><span class="kw3">undef</span></a>, <span class="st0">&#8216;persone&#8217;</span>, <span class="st0">&#8216;id&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$ins1</span>-&gt;<span class="me1">execute</span><span class="br0">&#40;</span> <span class="re0">$rv</span>, <span class="re0">$row</span>-&gt;<span class="br0">&#123;</span>address<span class="br0">&#125;</span>, <span class="re0">$row</span>-&gt;<span class="br0">&#123;</span>cap<span class="br0">&#125;</span>, <span class="re0">$row</span>-&gt;<span class="br0">&#123;</span>city<span class="br0">&#125;</span>, <span class="re0">$row</span>-&gt;<span class="br0">&#123;</span>country<span class="br0">&#125;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>&#8230;<span class="br0">&#93;</span><br />
&nbsp;<span class="br0">&#125;</span></div>
</div>
<p>La cosa interessante sta in quel <a href="http://search.cpan.org/~timb/DBI-1.609/DBI.pm#last_insert_id">last_insert_id</a> che raccatta l&#8217;ultimo valore auto incrementato e lo mette in uno scalare, riutilizzabile poi in altri insert per avere le foreign key allineate.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2010/02/16/conversione-rapida/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spostare e confrontare i dati</title>
		<link>http://www.simotrone.it/2010/02/14/spostare-e-confrontare-i-dati/</link>
		<comments>http://www.simotrone.it/2010/02/14/spostare-e-confrontare-i-dati/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 06:39:22 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[Mysql]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=2708</guid>
		<description><![CDATA[Dopo qualche ripensamento, ho deciso di spostare alcuni campi che stavano in una tabella (in MySQL) in un&#8217;altra. Come fare in maniera automatica?

UPDATE folks SET folks.birth = &#40; SELECT info.birth FROM info WHERE info.id = folks.id&#41;;

Se si vuole farlo solo per certe occorrenze, mettere un filtro WHERE in fondo per limitare gli update.
E se si [...]]]></description>
			<content:encoded><![CDATA[<p>Dopo qualche ripensamento, ho deciso di spostare alcuni campi che stavano in una tabella (in MySQL) in un&#8217;altra. Come fare in maniera automatica?</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw1">UPDATE</span> folks <span class="kw1">SET</span> folks.birth = <span class="br0">&#40;</span> <span class="kw1">SELECT</span> info.birth <span class="kw1">FROM</span> info <span class="kw1">WHERE</span> info.id = folks.id<span class="br0">&#41;</span>;</div>
</div>
<p>Se si vuole farlo solo per certe occorrenze, mettere un filtro WHERE in fondo per limitare gli update.</p>
<p>E se si vuole confrontare i dati e vedere se è tutto giusto?</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;"><span class="kw1">SELECT</span> <span class="kw1">IF</span><span class="br0">&#40;</span> STRCMP<span class="br0">&#40;</span>tab1.campo,tab2.campo<span class="br0">&#41;</span>, <span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">&#41;</span> <span class="kw1">FROM</span> tab1 <span class="kw1">JOIN</span> tab2 <span class="kw1">ON</span> tab1.id = tab2.id <span class="kw1">GROUP</span> <span class="kw1">BY</span> campo</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2010/02/14/spostare-e-confrontare-i-dati/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>mysqlshow</title>
		<link>http://www.simotrone.it/2010/02/10/mysqlshow/</link>
		<comments>http://www.simotrone.it/2010/02/10/mysqlshow/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 20:29:44 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[mysqlshow]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=2664</guid>
		<description><![CDATA[Lo sapevo che prima o poi mi avrebbe punito.
Ho imparato l&#8217;SQL e i db stando su MySQL, poi sono passato a Pg per varie ragioni (di lavoro ed altro).
Mi sono trovato assolutamente bene con Postgre &#8211; pur lottando ogni tanto con la sua macchinosità, e alla fine mi sono ritrovato a non usare mai MySQL&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>Lo sapevo che prima o poi mi avrebbe punito.<br />
Ho imparato l&#8217;SQL e i db stando su MySQL, poi sono passato a Pg per varie ragioni (di lavoro ed altro).<br />
Mi sono trovato assolutamente bene con Postgre &#8211; pur lottando ogni tanto con la sua macchinosità, e alla fine mi sono ritrovato a non usare mai MySQL&#8230; fino ad oggi. E ovviamente non mi ricordo (quasi) più nulla.</p>
<p>Poco male, si ricomincia. E due DB <em>is meil che one</em> (almeno finchè ci sono gli ORM come DBIC che capiscon tutto loro).</p>
<p>Mentre pistolavo a casaccio, ho visto che MySQL ha un tot di script da shell che mostrano un po&#8217; di robba, e comunque aiutano se li si impara ad usare.</p>
<pre>$ mysqlshow
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| agenda             |
| cmsfdt             |
| evento_odg         |
| libri              |
| mail               |
| mysql              |
| studiog7           |
| system_backup      |
| test               |
| test_blog          |
| wines              |
| wp281              |
+--------------------+

$ mysqlshow libri
Database: libri
+---------+
| Tables  |
+---------+
| book    |
| details |
+---------+

$ mysqlshow libri book
Database: libri  Table: book
+---------+-----------+-------------------+------+--
| Field   | Type      | Collation         | Null | ..
+---------+-----------+-------------------+------+--
| id      | int(11)   |                   | NO   | ..
| titolo  | char(100) | latin1_swedish_ci | NO   | ..
| autore  | char(100) | latin1_swedish_ci | NO   | ..
| editore | char(50)  | latin1_swedish_ci | NO   | ..
+---------+-----------+-------------------+------+---
</pre>
<p>A me è parso caruccio.<br />
 <img src='http://www.simotrone.it/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> </p>
<p>Edit postumo: mysqlshow ha un po&#8217; di opzioni. Una cosa che ho trovato utile è stato usare l&#8217;opzione &#8211;count che dice quante righe ha la tabella richiesta (ho aggiunto in ~/.bashrc alias mysqlshow=&#8217;mysqlshow &#8211;count&#8217;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2010/02/10/mysqlshow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL, API e C (2)</title>
		<link>http://www.simotrone.it/2009/01/24/mysql-api-e-c-2/</link>
		<comments>http://www.simotrone.it/2009/01/24/mysql-api-e-c-2/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 05:40:24 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Mysql]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=1184</guid>
		<description><![CDATA[Funzionozze per interrogare MySQL con il C.
La lista completa delle funzioni sul sito di MySQL.
Prototipo di mysql_init(), per allocare memoria e inizializzare l&#8217;oggetto MYSQL. 
proto: MYSQL *mysql_init(MYSQL *mysql)
Dopo aver inizializzato l&#8217;oggetto si puo&#8217; creare la connessione al db con mysql_real_connect().
proto: MYSQL *mysql_real_connect(MYSQL *mysql, const char *host,
     const char *user, const char [...]]]></description>
			<content:encoded><![CDATA[<p>Funzionozze per interrogare MySQL con il C.<br />
La <a href="http://dev.mysql.com/doc/refman/5.1/en/c-api-function-overview.html">lista completa delle funzioni</a> sul sito di MySQL.</p>
<p>Prototipo di <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-init.html">mysql_init()</a>, per allocare memoria e inizializzare l&#8217;oggetto MYSQL. </p>
<pre>proto: MYSQL *mysql_init(MYSQL *mysql)</pre>
<p>Dopo aver inizializzato l&#8217;oggetto si puo&#8217; creare la connessione al db con <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-real-connect.html">mysql_real_connect()</a>.</p>
<pre>proto: MYSQL *mysql_real_connect(MYSQL *mysql, const char *host,
     const char *user, const char *passwd, const char *db,
     unsigned int port, const char *unix_socket, unsigned long client_flag)</pre>
<p>Esempio: mysql_real_connect(&#038;mysql, host, user, pass, db_name, 0, NULL, 0)</p>
<p>La <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-real-query.html">query</a> si chiama col prototipo che segue inserendo la stringa <strong>stmt_str</strong> e la lunghezza della stessa (es.: con <strong>strlen()</strong>). Se ritorna zero, la query è avvenuta.</p>
<pre>proto:  int mysql_real_query(MYSQL *mysql, const char *stmt_str,
     unsigned long length)</pre>
<p>L&#8217;analisi del risultato è un po&#8217; macchinosa.</p>
<p>Il risultato della query viene incamerato grazie alla funzione <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-store-result.html">mysql_store_result()</a> che incamera i dati nella struct MYSQL_RES.</p>
<pre>proto: MYSQL_RES *mysql_store_result(MYSQL *mysql)</pre>
<p>Esempio:</p>
<pre>MYSQL mysql;
MYSQL_RES *result;

[ blah blah blah... ]
result = mysql_store_result(&#038;mysql)</pre>
<p>Notare che result rappresenta il risultato della query, sia esso</p>
<ol>
<li>Nullo perchè la query non è andata a buon fine</li>
<li>Con dati causati da un SELECT o uno SHOW.</li>
<li>Con risultato ma senza dati causato magari da un INSERT (che non spamma righe ma un risultato di righe modificate).</li>
</ol>
<p>La struct MYSQL_RES di result puo&#8217; venire esplorata con varie funzioni.</p>
<p>La funzione principale è probabilmente <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-fetch-row.html">mysql_fetch_row(result)</a> che estrae le singole righe dal result set.</p>
<pre>proto: MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)</pre>
<p>Il numero di valori nella riga (i campi restituiti) è dato da <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-num-fields.html">mysql_num_fields(result)</a> e <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-num-rows.html">mysql_num_rows()</a> conta le righe restituite.</p>
<pre>proto: unsigned int mysql_num_fields(MYSQL_RES *result)

proto: my_ulonglong mysql_num_rows(MYSQL_RES *result)</pre>
<p>Questo dovrebbe bastare per &#8220;navigare&#8221; i risultati della SELECT query (un bell&#8217;esempio per ciclare i risultati della query lo si trova <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-fetch-row.html">in fondo alla pagina del fetch</a>).</p>
<p>Per chiudere le operazioni, usare <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-free-result.html">mysql_free_result(result)</a> per liberare la memoria allocata per il <em>result set</em> e <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-close.html">mysql_close(&#038;mysql)</a>.</p>
<hr/>
Aggiuntine.</p>
<p><a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-fetch-fields.html">mysql_fetch_result(result)</a> parsa i campi.</p>
<pre>proto: MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result)

num_fields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
for(i = 0; i < num_fields; i++)
{
   printf("Field %u is %s\n", i, fields[i].name);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2009/01/24/mysql-api-e-c-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL, API e C (1)</title>
		<link>http://www.simotrone.it/2009/01/20/mysql-api-e-c/</link>
		<comments>http://www.simotrone.it/2009/01/20/mysql-api-e-c/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 05:46:42 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=1175</guid>
		<description><![CDATA[MySQL è the world&#8217;s most popular open source database.
C è un (il?) linguaggio di programmazione sviluppato da D. Ritchie per implementare Unix OS.
L&#8217;API è l&#8217;application programming interface.
Il file /usr/include/mysql/mysql.h è l&#8217;header che contiene classi, funzioni e variabili per mysql (#include &#60;mysql/mysql.h&#62;).
Il manualazzo delle API MySQL.
MYSQL è la struttura per maneggiare la connessione al database (le [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mysql.com/">MySQL</a> è <em>the world&#8217;s most popular open source database</em>.<br />
<a href="http://en.wikipedia.org/wiki/C_(programming_language)">C</a> è un (il?) linguaggio di programmazione sviluppato da <a href="http://www.cs.bell-labs.com/who/dmr/">D. Ritchie</a> per implementare Unix OS.<br />
L&#8217;<a href="http://en.wikipedia.org/wiki/API">API</a> è l&#8217;<em>application programming interface</em>.</p>
<p>Il file <b>/usr/include/mysql/mysql.h</b> è l&#8217;<a href="http://en.wikipedia.org/wiki/Header_files">header</a> che contiene classi, funzioni e variabili per mysql (<strong>#include &lt;mysql/mysql.h&gt;</strong>).</p>
<p>Il <a href="http://dev.mysql.com/doc/refman/5.1/en/c.html">manualazzo</a> delle API MySQL.</p>
<p><strong>MYSQL</strong> è la struttura per maneggiare la connessione al database (le si associa una variabile, e ne si richiama l&#8217;indirizzo con l&#8217;operatore unario <strong>&#038;</strong> ogni qual volta serva &#8211; cioè sempre).</p>
<pre>typedef struct st_mysql
{
  [... blah blah blah ...]
  char          *host,*user,*passwd,*unix_socket,*server_version,*host_info,*info;
  char          *db;
  struct charset_info_st *charset;
  MYSQL_FIELD   *fields;
  MEM_ROOT      field_alloc;
  my_ulonglong affected_rows;
  [... blah blah blah ...]
  my_bool       free_me;                /* If free in mysql_close */
  my_bool       reconnect;              /* set to 1 if automatic reconnect */

  /*
    Points to boolean flag in MYSQL_RES  or MYSQL_STMT. We set this flag
    from mysql_stmt_close if close had to cancel result set of this object.
  */
  my_bool *unbuffered_fetch_owner;
  [... blah blah blah ...]
} MYSQL;
</pre>
<p><strong>MYSQL_RES</strong> è la struttura rappresentante il risutlato di una query che ritorna delle righe (es. SELECT).</p>
<pre>typedef struct st_mysql_res {
  my_ulonglong row_count;
  MYSQL_FIELD   *fields;
  MYSQL_DATA    *data;
  MYSQL_ROWS    *data_cursor;
  unsigned long *lengths;               /* column lengths of current row */
  MYSQL         *handle;                /* for unbuffered reads */
  MEM_ROOT      field_alloc;
  unsigned int  field_count, current_field;
  MYSQL_ROW     row;                    /* If unbuffered read */
  MYSQL_ROW     current_row;            /* buffer to current row */
  my_bool       eof;                    /* Used by mysql_fetch_row */
  /* mysql_stmt_close() had to cancel this result */
  my_bool       unbuffered_fetch_cancelled;
  const struct st_mysql_methods *methods;
} MYSQL_RES;</pre>
<p><strong>MYSQL_ROW</strong> è un array che rappresenta una riga di dati.</p>
<pre>typedef char **MYSQL_ROW;               /* return data as array of strings */</pre>
<p>Ci metto pure il <strong>MYSQL_FIELD</strong>, visto che viene considerato dalla struct RES.</p>
<pre>typedef struct st_mysql_field {
  char *name;                 /* Name of column */
  char *org_name;             /* Original column name, if an alias */
  char *table;                /* Table of column if column was a field */
  char *org_table;            /* Org table name, if table was an alias */
  char *db;                   /* Database for table */
  char *catalog;              /* Catalog for table */
  char *def;                  /* Default value (set by mysql_list_fields) */
  [... blah blah blah ...]
  enum enum_field_types type; /* Type of field. See mysql_com.h for types */
} MYSQL_FIELD;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2009/01/20/mysql-api-e-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>dpkg dpkg!</title>
		<link>http://www.simotrone.it/2009/01/15/dpkg-dpkg/</link>
		<comments>http://www.simotrone.it/2009/01/15/dpkg-dpkg/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 11:13:51 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[dpkg]]></category>
		<category><![CDATA[Mysql]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/2009/01/15/dpkg-dpkg/</guid>
		<description><![CDATA[Stordito come una capra cascata di testa su di un masso, mi dimentico sempre qualcosa&#8230;
Ora, attraverso questo post-catarsi after figguddemmerd mi segno:
> dpkg -l &#124; grep qualcosa_di_simile_a_nome_pacchetto
> dpkg -s nome_pacchetto
> dpkg -L nome_pacchetto
-L per vedere i files installati!!! Orco cane!
/usr/include/mysql/mysql.h
&#8230;e poi guardo quel che mi pare!
]]></description>
			<content:encoded><![CDATA[<p>Stordito come una capra cascata di testa su di un masso, mi dimentico sempre qualcosa&#8230;<br />
Ora, attraverso questo post-catarsi after figguddemmerd mi segno:<br />
> dpkg -l | grep qualcosa_di_simile_a_nome_pacchetto<br />
> dpkg <strong>-s</strong> nome_pacchetto<br />
> dpkg <strong>-L</strong> nome_pacchetto</p>
<p>-L per vedere i files installati!!! Orco cane!<br />
/usr/include/mysql/<strong>mysql.h</strong><br />
&#8230;e poi guardo quel che mi pare!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2009/01/15/dpkg-dpkg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LAMP</title>
		<link>http://www.simotrone.it/2008/05/20/lamp/</link>
		<comments>http://www.simotrone.it/2008/05/20/lamp/#comments</comments>
		<pubDate>Tue, 20 May 2008 17:51:25 +0000</pubDate>
		<dc:creator>Simotrone</dc:creator>
				<category><![CDATA[Binary People]]></category>
		<category><![CDATA[Rhythm of my life]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Linking Park]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[OST]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Transformer]]></category>

		<guid isPermaLink="false">http://www.simotrone.it/?p=548</guid>
		<description><![CDATA[Linux
Apache
MySQL
Php (e altro, nel mio caso)
Sottofondo musicale: OST dei Transformer. Da paura.
1. Linkin Park &#8211; What I&#8217;ve Done
2. Smashing Pumpkins &#8211; Doomsday Clock
3. Disturbed &#8211; This Moment
4. Goo Goo Dolls &#8211; Before It&#8217;s Too Late (Sam And Mikaela&#8217;s Theme)
5. The Used &#8211; Pretty Handsome Awkward
6. HIM &#8211; Passion&#8217;s Killing Floor
7. Taking Back Sunday &#8211; What [...]]]></description>
			<content:encoded><![CDATA[<p><img align="right" src="/wp-content/Img_generic/Transformers_2007_OST.jpg" alt="OST Transformer" /><strong>L</strong>inux<br />
<a href="http://httpd.apache.org/"><strong>A</strong>pache</a><br />
<a href="http://www.mysql.com/"><strong>M</strong>ySQL</a><br />
<a href="http://www.php.net/"><strong>P</strong>hp</a> (e altro, nel mio caso)</p>
<p>Sottofondo musicale: <strong>OST dei Transformer</strong>. Da paura.</p>
<blockquote><p>1. <em>Linkin Park</em> &#8211; What I&#8217;ve Done<br />
2. <em>Smashing Pumpkins</em> &#8211; Doomsday Clock<br />
3. <em>Disturbed</em> &#8211; This Moment<br />
4. <em>Goo Goo Dolls</em> &#8211; Before It&#8217;s Too Late (Sam And Mikaela&#8217;s Theme)<br />
5. <em>The Used</em> &#8211; Pretty Handsome Awkward<br />
6. <em>HIM</em> &#8211; Passion&#8217;s Killing Floor<br />
7. <em>Taking Back Sunday</em> &#8211; What It Feels Like To Be A Ghost?<br />
8. <em>Styles Of Beyond Feat. Mike Shinoda</em> &#8211; Second To None<br />
9. <em>Armor For Sleep</em> &#8211; End Of The World<br />
10. <em>Idiot Pilot</em> &#8211; Retina And The Sky<br />
11. <em>Julien-K</em> &#8211; Technical Difficulties<br />
12. <em>Mutemath</em> &#8211; Transformers Theme</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.simotrone.it/2008/05/20/lamp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
