<?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>Saturnboy &#187; UTF-8</title>
	<atom:link href="http://saturnboy.com/tag/utf-8/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Thu, 19 Aug 2010 03:28:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Like Peas and Carrots: LCDS 3 and UTF-8</title>
		<link>http://saturnboy.com/2010/02/lcds-and-utf-8/</link>
		<comments>http://saturnboy.com/2010/02/lcds-and-utf-8/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 17:54:32 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[LCDS]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=958</guid>
		<description><![CDATA[I just wrote about how to handle special characters in Flex 4 when written as HTML entities in MXML. Now I&#8217;ve moved my data with the special characters out of MXML and down into a MySQL database. Data access is provided by a vanilla LCDS 3 backend. I now have a very different problem than [...]]]></description>
			<content:encoded><![CDATA[<p>I just wrote about how to handle <a href="http://saturnboy.com/2010/01/special-characters-flex-4/">special characters in Flex 4</a> when written as HTML entities in MXML. Now I&#8217;ve moved my data with the special characters out of MXML and down into a MySQL database.  Data access is provided by a vanilla <a href="http://www.adobe.com/products/livecycle/dataservices/">LCDS 3</a> backend. I now have a very different problem than what I had before: How do I get UTF-8 data out of the database with LCDS and onto the display?</p>
<h5>MySQL and UTF-8</h5>
<p>In theory, LCDS is perfectly happy with special characters and foreign languages (here&#8217;s a link to <a href="http://help.adobe.com/en_US/livecycle/9.0/programLC/langref/charset-codes.html">supported characters sets</a> in LiveCycle ES2). So this time around, our problem has nothing to do with Flex 4 or LCDS, instead it&#8217;s all about the database. For our example, we&#8217;ll skip the pure model driven development route and just start with a simple database with a single <code>players</code> table.</p>
<p class="bottom">Create the database:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> ballerz <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARACTER <span style="color: #993333; font-weight: bold;">SET</span> utf8 COLLATE utf8_general_ci;
<span style="color: #993333; font-weight: bold;">CREATE</span> USER <span style="color: #ff0000;">'baller'</span>@<span style="color: #ff0000;">'localhost'</span> <span style="color: #993333; font-weight: bold;">IDENTIFIED</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">'password'</span>;
<span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">ALL</span> PRIVILEGES <span style="color: #993333; font-weight: bold;">ON</span> ballerz<span style="color: #66cc66;">.*</span> <span style="color: #993333; font-weight: bold;">TO</span> baller@localhost;</pre></div></div>

<p>It is possible to configure MySQL to default to UTF-8 friendly behavior but the <code>CREATE DATABASE</code> command guarantees that the newly created db will be happy.</p>
<p class="bottom">Create the <code>players</code> table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> players <span style="color: #66cc66;">&#40;</span>
  id int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Nothing special here, just use <code>VARCHAR</code> for the text fields.  In this case, we only have the player&#8217;s name.</p>
<p class="bottom">Insert some sample data:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Carmelo Anthony&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Chaunçey Billups&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Nenê&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Këñÿõn Martin&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;LeBrøn James&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Mo Williams&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Shaquille O†Neal&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Ænderson Varejao&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Zýdrunãs Ílgauskãs&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I put some extra special characters into the <code>INSERT</code> statements just for fun.</p>
<blockquote class="deeper"><p><b>Digging Deeper:</b> When connecting to MySQL from the commandline you can use the <code>--default-character-set=utf8</code> option to force your terminal to show UTF-8 characters correctly.</p></blockquote>
<h5>LCDS and UTF-8</h5>
<p>Now that the database is correctly setup to handle UTF-8, the rest of the LCDS setup is straight forward (see my getting started <a href="http://saturnboy.com/2009/07/get-started-lcds-mysql-1/">part 1</a> and <a href="http://saturnboy.com/2009/07/get-started-lcds-mysql-2/">part 2</a> posts). Create a new LCDS webapp via copy-and-paste from the template app, then fire up Flash Builder 4 and get to work.  In the Modeler plugin, configure a new RDS connection and just drag-and-drop the players table into the model.</p>
<p class="bottom">Here&#8217;s a screenshot of our LCDS model:</p>
<div class="span-14 last" style="min-height:274px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/01/lcds_utf8_model.png" alt="model" width="391" height="264" />
</div>
<p class="bottom">And here&#8217;s a screenshot of the running app (remember this is backed by LCDS, so no running demo):</p>
<div class="span-14 last" style="min-height:180px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/01/lcds_utf8_screenshot.png" alt="screenshot" width="390" height="170" />
</div>
<p class="bottom">Lastly, the frontend code showing just the highlights:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> ...</span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;complete()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">           private function complete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                getPlayers.token = playerService.getAll<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:CallResponder</span> id=<span style="color: #ff0000;">&quot;getPlayers&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;Ballerz:PlayerService</span> id=<span style="color: #ff0000;">&quot;playerService&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> </span>
<span style="color: #000000;">            dataProvider=<span style="color: #ff0000;">&quot;{getPlayers.lastResult}&quot;</span></span>
<span style="color: #000000;">            labelField=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Again, no magic here, I use a simple <code>getAll()</code> query to retrieve the entire <code>players</code> table then feed it into a <code>List</code> via the <code>CallResponder</code>&#8216;s <code>lastResult</code> property.</p>
<h5>Conclusion</h5>
<p>So the moral of our story is: if you correctly configure your database to support UTF-8 and you correctly get UTF-8 data into your tables, then everything just works.  LCDS will transparently get data out of the db and Flex will transparently get it onto the screen.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/Ballerz.tgz">Ballerz.tgz</a></li>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/create_db.sql">create_db.sql</a></li>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/create_tables.sql">create_tables.sql</a></li>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/data.sql">data.sql</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/02/lcds-and-utf-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
