<?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>I want to be free &#187; db</title>
	<atom:link href="http://i1t2b3.com/category/db/feed/" rel="self" type="application/rss+xml" />
	<link>http://i1t2b3.com</link>
	<description>Any fool can make things bigger and more complex</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:24:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Delete Your Code #7: Right encoding</title>
		<link>http://i1t2b3.com/2012/02/02/delete-your-code-right-encoding/</link>
		<comments>http://i1t2b3.com/2012/02/02/delete-your-code-right-encoding/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 15:19:59 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[delete your code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=835</guid>
		<description><![CDATA[You might be surprised, but a right choise of the project text encoding can affect the project file size and amount of bugs. To avoid bugs of wrong presentation of text on your page, make sure that all database entities and the application server (PHP) use the same encoding. That helps to forget about issues [...]]]></description>
			<content:encoded><![CDATA[<p>You might be surprised, but a right choise of the project text encoding can affect the project file size and amount of bugs.</p>
<p>To avoid bugs of wrong presentation of text on your page, make sure that all database entities and the application server (PHP) use the same encoding. That helps to forget about issues connected with text presentation.</p>
<p style="text-align: center;"><img class="size-medium wp-image-837 aligncenter" title="bart simpson utf8" src="http://i1t2b3.com/wp-content/uploads/2012/02/bart-simpson-utf81-300x160.gif" alt="" width="300" height="160" /></p>
<p>On database side, make sure you set the correct encoding to:</p>
<ul>
<li>database,</li>
<li>tables,</li>
<li>columns,</li>
<li>import-export tools parameters (a big source of wrong encoding bugs),</li>
<li>corresponding SQL server variables</li>
</ul>
<p>On application server it&#8217;s usually just one query &#8211;
<pre><code class="sql">SET NAMES utf8</code></pre>
<p>Pay attention, that utf8 might be not the best choise for your project: every non-English character needs 2-6 bytes of memory, so if you built a one-language (local) project with lots of database data, consider using a 1 byte encoding like windows-1251 and save about half of the space on server file system.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2012/02/02/delete-your-code-right-encoding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete Your Code #5: Use foreign keys</title>
		<link>http://i1t2b3.com/2011/12/16/delete-your-code-use-f/</link>
		<comments>http://i1t2b3.com/2011/12/16/delete-your-code-use-f/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 21:52:45 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[delete your code]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=818</guid>
		<description><![CDATA[A foreign key, this classic concept of relational databases, is extremely helpful in many ways. To start with, it&#8217;s a watch dog of your database integrity — for example, it would be impossible to add user images records, if the user does not exist. No additional code, more control. Another benefit is that all depending [...]]]></description>
			<content:encoded><![CDATA[<p>A foreign key, this classic concept of relational databases, is extremely helpful in many ways.</p>
<p><img src="http://i1t2b3.com/wp-content/uploads/2011/12/on-delete-cascade.jpg" alt="" title="on delete cascade" width="200" height="200" class="alignright size-full wp-image-819" />To start with, it&#8217;s a watch dog of your database integrity — for example, it would be impossible to add user images records, if the user does not exist. No additional code, more control.</p>
<p>Another benefit is that all depending data is deleted cascadedly when the parent row gets killed. You remove a user — and voila: all images, comments, ratings and staff fly away too.</p>
<p>And third, it helps to autogenerate admin tools; for instance, in Symfony framework, the generator of admin interface relies on this info.</p>
<p>To be able to use foreign keys in your MySQL database, do 2 following steps:</p>
<ul>
<li>make sure the two tables you are connecting, are driven by InnoDB</li>
<li>ensure that the columns you connect (<code>user_id</code> in our example) are of exactly the same type, including NULL and sign allowance.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/12/16/delete-your-code-use-f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detect if city is within area</title>
		<link>http://i1t2b3.com/2011/12/13/detect-if-city-is-within-area/</link>
		<comments>http://i1t2b3.com/2011/12/13/detect-if-city-is-within-area/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 18:58:36 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[geo]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=778</guid>
		<description><![CDATA[We at SunnyRentals are working a lot with geo data. The last task I was solving was inclusion of all cities in a touristic regions bounds automatically. Yes, there are MySQL spatial data types, so region border and cities coordinates can be stored in native data types, but the processing functions are not implemented right: [...]]]></description>
			<content:encoded><![CDATA[<p>We at SunnyRentals are working a lot with geo data. </p>
<p>The last task I was solving was inclusion of all cities in a touristic regions bounds automatically.</p>
<p>Yes, there are <a rel="nofollow" href="http://dev.mysql.com/doc/refman/5.1/en/mysql-spatial-datatypes.html">MySQL spatial data types</a>, so region border and cities coordinates can be stored in native data types, but the processing functions are not implemented right: they only operate on MBRs (minimum bounding rectangles) to make things simpler&#8230;</p>
<p><img src="http://i1t2b3.com/wp-content/uploads/2011/12/284_theory_fig_one1.png" alt="" title="point in polygon" width="200" height="148" class="alignright size-full wp-image-793" />So, let&#8217;s create our own MySQL function for that.</p>
<p>There are different approaches to distinguish if a point is within a given polygon. I prefered the way to iterate the edges of the polygon and check where the point relatively to the edge is &#8211; at right or at left; if the point is always at the same side, it lies within the border.</p>
<p>So we came to a fact that we need 2 functions:</p>
<ol>
<li>Distinguish the side at which the point lies from the line</li>
<li>Check that for every edge in the polygon.</li>
</ol>
<p>Here is the first function: <code class="sql">
<pre>DELIMITER //

DROP FUNCTION `GetPointPositionOfLine`//
CREATE FUNCTION `GetPointPositionOfLine`( a POINT, b POINT, p POINT) RETURNS tinyint NO SQL
  COMMENT 'Distinguish the side at which the point lies from the line'
BEGIN
  DECLARE s DOUBLE;
  SET s = (X(b) - X(a))*(Y(p) - Y(a)) - (Y(b) - Y(a))*(X(p) - X(a));
  IF s &gt; 0 THEN
    RETURN 1;
  END IF;
  IF s &lt; 0 THEN
    RETURN -1;
  END IF;
  RETURN 0;
END //</pre>
<p></code><br />
All three params are of type POINT. Points <code>a</code> and <code>b</code> are the beginning and end of the line. Point <code>p</code> is the point we check.</p>
<p>Here is the main function: <code class="sql">
<pre>DELIMITER //

DROP FUNCTION `IsPointWithin`//
CREATE FUNCTION `IsPointWithin`( ls LINESTRING, p POINT) RETURNS tinyint(1) NO SQL
BEGIN
  DECLARE i INT;
  DECLARE a, b POINT;
  DECLARE LineStringNumPoints INT;
  DECLARE Position, PrevPosition TINYINT;

  SET i = 1;
  SET PrevPosition = 0;
  SET LineStringNumPoints = NumPoints(ls);

  WHILE i &lt;= LineStringNumPoints DO
    SET a = PointN( ls, i);
    IF i = LineStringNumPoints THEN
      SET b = StartPoint( ls);
    ELSE
      SET b = PointN( ls, i+1);
    END IF;

    SET Position = GetPointPositionOfLine( a, b, p);
    IF Position &lt;&gt; 0 THEN
      IF Position &lt;&gt; PrevPosition AND PrevPosition &lt;&gt; 0 THEN
        RETURN FALSE;
      END IF;
      SET PrevPosition = Position;
    END IF;
    SET i = i + 1;
  END WHILE;
  RETURN TRUE;
END //</pre>
<p></code><br />
It checks if a point <code>p</code> lies within a polygon of type LINESTRING.</p>
<p>Usage example. Given you have coordinates of cities in the separate float columns (to keep them readable) <code>lat</code> and <code>lng</code> of <code>city</code> table, and the polygon is given as a string too &mdash; but of course you can use a column of type LINESTRING:<br />
<code class="sql">
<pre>SELECT *
FROM `city` c
WHERE IsPointWithin(GeomFromText('LINESTRING(4 8, 15 16, 23 42)'), POINT(c.`lat`, c.`lng`));
</pre>
<p></code></p>
<p>The solution works pretty fast, especially if you first limit the cities by the rectangle that the polygon lies in.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/12/13/detect-if-city-is-within-area/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Proxy</title>
		<link>http://i1t2b3.com/2011/06/06/mysql-proxy/</link>
		<comments>http://i1t2b3.com/2011/06/06/mysql-proxy/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 10:15:24 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=382</guid>
		<description><![CDATA[At times it&#8217;s necessary to know which queries the server runs right now. Of course, you can try this statement: SHOW PROCESSLIST but if you need a more poweful tool, try MySQL Proxy. Although it&#8217;s still an alpha version, it&#8217;s a handy tool to monitor, filter and manipulate your queries since there is Lua scripting [...]]]></description>
			<content:encoded><![CDATA[<p>At times it&#8217;s necessary to know which queries the server runs right now.</p>
<p>Of course, you can try this statement:
<pre><code>SHOW PROCESSLIST</code></pre>
<p> but if you need a more poweful tool, try <noindex><a rel="nofollow" href="http://dev.mysql.com/downloads/mysql-proxy/">MySQL Proxy</a></noindex>.</p>
<p>Although it&#8217;s still an alpha version, it&#8217;s a handy tool to monitor, filter and manipulate your queries since there is Lua scripting language interpreter supported which is quite obvious in usage.</p>
<p>The MySQL Proxy (as can be understood from its title) can help you to see communication between a few MySQL servers and a few clients.</p>
<p>My usage pattern is that the Proxy listens to 4040 port on my local machine, and if I change the port setting in connection string of any application (my job project or even PhpMyAdmin), I can see the queries logged in a console window.</p>
<p>You can track lots of characteristics of queries &mdash; just take a look at example Lua scripts in the package that you&#8217;ve downloaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/06/06/mysql-proxy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bulk SQL loading</title>
		<link>http://i1t2b3.com/2011/05/30/bulk-sql-loading/</link>
		<comments>http://i1t2b3.com/2011/05/30/bulk-sql-loading/#comments</comments>
		<pubDate>Mon, 30 May 2011 13:59:50 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=349</guid>
		<description><![CDATA[If you want to load a list of SQL files into your database on Windows, you can create a .cmd file with such content and run it. Place it in the same folder where your .sql files are. @echo off SET DATABASE=my_database SET USER=root SET PASS=1 SET FORMAT=*.sql FOR /F "usebackq" %%i IN (`dir /on [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to load a list of SQL files into your database on Windows, you can create a .cmd file with such content and run it.<br />
Place it in the same folder where your .sql files are.</p>
<pre><code class="dos">@echo off
SET DATABASE=my_database
SET USER=root
SET PASS=1
SET FORMAT=*.sql

FOR /F "usebackq" %%i IN (`dir /on /b %FORMAT%`) DO echo %%i &amp;&amp; mysql --user=%USER% --password=%PASS% --default-character-set=utf8 %DATABASE% &lt; %%i</code></pre>
<p>Make sure, <code>mysql</code> command can be run. If no, either set the full path to mysql command tool, or add the path to PATH environment variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/05/30/bulk-sql-loading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sphinx pre-query not inheriting</title>
		<link>http://i1t2b3.com/2011/04/13/sphinx-pre-query-not-inheriting/</link>
		<comments>http://i1t2b3.com/2011/04/13/sphinx-pre-query-not-inheriting/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 09:57:22 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=675</guid>
		<description><![CDATA[Sphinx is a nice search engine and is used at our project hugely. Although, I found a case when using a pre-fetch query is not obvious. The pre-fetch query is a query that is run before the data is got from database, so thus you can set character encoding, tune database caching, set variables, etc. [...]]]></description>
			<content:encoded><![CDATA[<p>Sphinx is a nice search engine and is used at our project hugely.</p>
<p>Although, I found a case when using a pre-fetch query is not obvious. The pre-fetch query is a query that is run before the data is got from database, so thus you can set character encoding, tune database caching, set variables, etc.</p>
<p>So, if we have 2 sources, one is parent for all other sources since it has database connection params, and second one inherits from the parent.</p>
<pre><code>source www {
    type                    = mysql

    sql_user                = root
    sql_pass                = 

    sql_query_pre           = SET NAMES utf8
}

source www_country : www {
    sql_query = SELECT * FROM country
}
</code></pre>
<p>All works fine &#8211; when <code>www_country</code> souce is used, the same <code>sql_query_pre</code> will be used because <code>www_country</code> is a child of parent source.</p>
<p>Although, if one day you decide to add a custom pre-query to the child source, it seems you loose the parent&#8217;s ones:</p>
<pre><code>...
source www_country : www {
    <strong>sql_query_pre = SET @usefulVariable = 1</strong>

    sql_query = SELECT * FROM country
}
</code></pre>
<p>In this case it seems you have to duplicate the parental pre-query in every child =(<br />
So the right child is gonna be:</p>
<pre><code>...
source www_country : www {
    sql_query_pre = SET NAMES utf8
    sql_query_pre = SET @usefulVariable = 1

    sql_query = SELECT * FROM country
}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/04/13/sphinx-pre-query-not-inheriting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reverse geocoding in your app</title>
		<link>http://i1t2b3.com/2010/11/17/reverse-geocoding-in-your-app/</link>
		<comments>http://i1t2b3.com/2010/11/17/reverse-geocoding-in-your-app/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 12:15:41 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=659</guid>
		<description><![CDATA[Reverse geocoding is a process of getting toponym name (city name) by its coordinates. To make this, you can use a remote service like I have already described, or implement it yourself. To do so you need two things: A database of cities with coordinates (having a population value at least for major cities is [...]]]></description>
			<content:encoded><![CDATA[<p>Reverse geocoding is a process of getting toponym name (city name) by its coordinates.</p>
<p>To make this, you can use a remote service like I <a href="/2009/09/06/reverse-geocoding/">have already described</a>, or implement it yourself.</p>
<p>To do so you need two things:</p>
<ol>
<li>A database of cities with coordinates (having a population value at least for major cities is a big plus)</li>
<li>A method to find a closest city</li>
</ol>
<p>#1 is left for you, let&#8217;s talk about #2.</p>
<p>In the <a href="/2010/11/04/sql-function-to-count-distance-between-two-points/">previous post</a> I introduced the DISTANCE function (SQL) to count a distance between 2 points by coordinates. Let&#8217;s use it to find the biggest city closest to a given point:</p>
<pre><code>DELIMITER //
DROP FUNCTION IF EXISTS REVERSE_GEOCODE;//
CREATE FUNCTION REVERSE_GEOCODE( latitude DOUBLE, longitude DOUBLE, radius DOUBLE )
    RETURNS DOUBLE READS SQL DATA DETERMINISTIC
    COMMENT 'Finds a city by coordinates given. Returns City ID.'
BEGIN
    DECLARE cityID INT(10) DEFAULT 0;
    DECLARE d DOUBLE DEFAULT 0; /* useless var */

    SELECT id, DISTANCE(`lat`, `lng`, latitude, longitude) AS dist
    INTO cityID, d
    FROM `geo_cities`
    WHERE lat BETWEEN FLOOR(latitude - 0.5)
                  AND CEIL(latitude  + 0.5)
      AND lng BETWEEN FLOOR(longitude- 0.5)
                  AND CEIL(longitude + 0.5)
    HAVING dist &lt; radius
    ORDER BY IF(population &gt; 0, population, 1) * (1/IF(dist &gt; 0, dist, 1)) DESC
    LIMIT 1;

    RETURN cityID;
END; //
DELIMITER ;
</code></pre>
<p>Well, this function orders cities by (population and distance) function &mdash; so that the biggest closest city is returned.</p>
<p>WHERE clause is supposed to shorten the number of cities which are looked through; otherwise we would have to search through whole world&#8217;s cities. In my solution the search is limited by a reasonably small rectangle.</p>
<p>In the next post I&#8217;ll tell you how to find a biggest city depending on current Google Maps zoom, so that it won&#8217;t give you a small city near Paris if you click on France at a world-level map view.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2010/11/17/reverse-geocoding-in-your-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL function to count distance between two points</title>
		<link>http://i1t2b3.com/2010/11/04/sql-function-to-count-distance-between-two-points/</link>
		<comments>http://i1t2b3.com/2010/11/04/sql-function-to-count-distance-between-two-points/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 15:49:48 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=654</guid>
		<description><![CDATA[Useful function. DELIMITER // DROP FUNCTION IF EXISTS DISTANCE; // CREATE FUNCTION DISTANCE( lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE ) RETURNS DOUBLE NO SQL DETERMINISTIC COMMENT 'counts distance (km) between 2 points on Earth surface' BEGIN DECLARE dtor DOUBLE DEFAULT 57.295800; RETURN (6371 * acos(sin( lat1/dtor) * sin(lat2/dtor) + cos(lat1/dtor) * cos(lat2/dtor) * [...]]]></description>
			<content:encoded><![CDATA[<p>Useful function.</p>
<pre><code>DELIMITER //

DROP FUNCTION IF EXISTS DISTANCE; //

CREATE FUNCTION DISTANCE( lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE )
    RETURNS DOUBLE NO SQL DETERMINISTIC
    COMMENT 'counts distance (km) between 2 points on Earth surface'
BEGIN
    DECLARE dtor DOUBLE DEFAULT 57.295800;

    RETURN (6371 * acos(sin( lat1/dtor) * sin(lat2/dtor) +
        cos(lat1/dtor) * cos(lat2/dtor) *
        cos(lon2/dtor - lon1/dtor)));
END; //

DELIMITER ;
</code></pre>
<p>In next post I will explain how I used this function to implement a reverse geocoding &mdash; it&#8217;s useful when you need to find a closest city by given coordinates.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2010/11/04/sql-function-to-count-distance-between-two-points/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Preferrable sorting in MySQL</title>
		<link>http://i1t2b3.com/2010/11/02/preferrable-sorting-in-mysql/</link>
		<comments>http://i1t2b3.com/2010/11/02/preferrable-sorting-in-mysql/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 08:44:29 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=587</guid>
		<description><![CDATA[At times, some records in a recordset are more important than others &#8212; e.g. a featured product in a products list. In such a case you can make 2 requests to get the featured item first, then to load other N-1 items, and then display them. If you want to make it by one move, [...]]]></description>
			<content:encoded><![CDATA[<p>At times, some records in a recordset are more important than others &mdash; e.g. a featured product in a products list. In such a case you can make 2 requests to get the featured item first, then to load other N-1 items, and then display them.</p>
<p>If you want to make it by one move, you can use a feature of preferrable sorting  &mdash; you can define what value and in which field goes first if such value is present.</p>
<p>For instance, you want to show an item with item_id=15 first:</p>
<pre><code>SELECT *
FROM items
ORDER BY item_id IN (15) DESC</code></pre>
<p>Which returns something like (featured item goes first):</p>
<pre><code>+----------+------------+
| item_id  | name       |
+----------+------------+
| <strong>15</strong>       | <strong>Product 15</strong> |
| 32       | Product 32 |
| 31       | Product 31 |
| 30       | Product 30 |
| 29       | Product 29 |
| ...      | ...        |
+----------+------------+</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2010/11/02/preferrable-sorting-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert latin1 to utf8</title>
		<link>http://i1t2b3.com/2010/04/14/convert-latin1-to-utf8/</link>
		<comments>http://i1t2b3.com/2010/04/14/convert-latin1-to-utf8/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 12:51:35 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=598</guid>
		<description><![CDATA[If you import unicode text into latin1 database column, the symbols would be screwed up — russian symbols become a shit like &#8220;Ð·Ð°Ð»Ð¾Ð³Ð¾Ð²Ñ‹Ð¹ Ð´ÐµÐ¿Ð¾Ð·Ð¸Ñ‚&#8221;. To convert such quickly (as a test) you can use Lebedev&#8217;s convertor. To convert the whole table do the following (thanks to dull.ru): The most important step: dump such data in [...]]]></description>
			<content:encoded><![CDATA[<p>If you import unicode text into latin1 database column, the symbols would be screwed up — russian symbols become a shit like &#8220;Ð·Ð°Ð»Ð¾Ð³Ð¾Ð²Ñ‹Ð¹ Ð´ÐµÐ¿Ð¾Ð·Ð¸Ñ‚&#8221;.</p>
<p><img class="aligncenter size-full wp-image-599" title="extras" src="/wp-content/uploads/2010/04/extras.png" alt="" width="484" height="218" /></p>
<p>To convert such quickly (as a test) you can use <noindex><a rel="nofollow" rel="nofollow" href="http://www.artlebedev.ru/tools/decoder/">Lebedev&#8217;s convertor</a></noindex>.</p>
<p>To convert the whole table do the following (thanks to <noindex><a rel="nofollow" rel="nofollow" href="http://dull.ru/2008/11/06/konvertirovat_bazu_v_utf8/">dull.ru</a></noindex>):</p>
<ol>
<li>The most important step: dump such data in a file with <code>mysqldump</code>
<pre><code>mysqldump -u user -p <strong>--default-character-set=latin1 </strong>--skip-set-charset --no-create-info --extended-insert --complete-insert dbname table &gt; dbname.sql</code></pre>
<p>If not the whole table data is bad, create another table (<code>CREATE TABLE t2 LIKE t1</code>) and copy wrong rows to the new table.</li>
<li>Replace <code>latin1</code> by <code>utf8</code> in the file<br />
This can be done manually in your editor or by this command:</p>
<pre><code>sed -r 's/latin1/utf8/g' dbname.sql &gt; dbname_utf.sql</code></pre>
</li>
<li>Convert your latin1-table to utf8 (and maybe truncate it):
<pre><code>ALTER TABLE `table` CONVERT TO CHARACTER SET 'utf8';</code></pre>
</li>
<li>Import the utf8 data back to the table:
<pre><code>mysql -u user -p --default-character-set=utf8 dbname &lt; dbname_utf.sql</code></pre>
</li>
</ol>
<p>That&#8217;s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2010/04/14/convert-latin1-to-utf8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

