I just wrote about how to handle special characters in Flex 4 when written as HTML entities in MXML. Now I’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 what I had before: How do I get UTF-8 data out of the database with LCDS and onto the display?
MySQL and UTF-8
In theory, LCDS is perfectly happy with special characters and foreign languages (here’s a link to supported characters sets in LiveCycle ES2). So this time around, our problem has nothing to do with Flex 4 or LCDS, instead it’s all about the database. For our example, we’ll skip the pure model driven development route and just start with a simple database with a single players table.
Create the database:
CREATE DATABASE ballerz DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 'baller'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON ballerz.* TO baller@localhost;
It is possible to configure MySQL to default to UTF-8 friendly behavior but the CREATE DATABASE command guarantees that the newly created db will be happy.
Create the players table:
CREATE TABLE players ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255), PRIMARY KEY (id) );
Nothing special here, just use VARCHAR for the text fields. In this case, we only have the player’s name.
Insert some sample data:
INSERT INTO players (id,name) VALUES (1, "Carmelo Anthony"); INSERT INTO players (id,name) VALUES (2, "Chaunçey Billups"); INSERT INTO players (id,name) VALUES (3, "Nenê"); INSERT INTO players (id,name) VALUES (4, "Këñÿõn Martin"); INSERT INTO players (id,name) VALUES (5, "LeBrøn James"); INSERT INTO players (id,name) VALUES (6, "Mo Williams"); INSERT INTO players (id,name) VALUES (7, "Shaquille O†Neal"); INSERT INTO players (id,name) VALUES (8, "Ænderson Varejao"); INSERT INTO players (id,name) VALUES (9, "Zýdrunãs Ílgauskãs");
I put some extra special characters into the INSERT statements just for fun.
Digging Deeper: When connecting to MySQL from the commandline you can use the
--default-character-set=utf8option to force your terminal to show UTF-8 characters correctly.
LCDS and UTF-8
Now that the database is correctly setup to handle UTF-8, the rest of the LCDS setup is straight forward (see my getting started part 1 and part 2 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.
Here’s a screenshot of our LCDS model:
And here’s a screenshot of the running app (remember this is backed by LCDS, so no running demo):
Lastly, the frontend code showing just the highlights:
<?xml version="1.0" encoding="utf-8"?> <s:Application ... creationComplete="complete()"> <fx:Script> <![CDATA[ private function complete():void { getPlayers.token = playerService.getAll(); } ]]> </fx:Script> <fx:Declarations> <s:CallResponder id="getPlayers" /> <Ballerz:PlayerService id="playerService" /> </fx:Declarations> <s:List dataProvider="{getPlayers.lastResult}" labelField="name" /> </s:Application>
Again, no magic here, I use a simple getAll() query to retrieve the entire players table then feed it into a List via the CallResponder‘s lastResult property.
Conclusion
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.
