MySQL to pure utf-8 character set configuration

I’m programming a new version of my Japanese translator in the python Django framework as practice. Aside from some hard lessons, such as you cannot install a Python 2.5 x64 binary in Windows and expect the mod_python installer to work, the following was my next problem:

Had some trouble importing a file with Japanese in UTF-8 encoding into mySQL. The import would go fine, then would completely come out garbage on output. I eventually figured out what to do. Edit the my.cnf for the server, and use the following parameters:


[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'

[mysql]
default-character-set=utf8

Now, if I did the following in mySQL:


mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8                           |
| character_set_connection | utf8                           |
| character_set_filesystem | binary                         |
| character_set_results    | utf8                           |
| character_set_server     | utf8                           |
| character_set_system     | utf8                           |
| character_sets_dir       | C:xamppmysqlsharecharsets |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

If everything is utf8, then you are golden!

Hi, I create problems.

For the past month, on and off, I’ve been thinking about problem-solving – there’s too much of it. Everyone’s a problem solver, but since when did you hear someone openly say that they create problems? I think the relationship between a problem and solution is cyclical – a problem is a consequence of a solution, and a solution is a consequence of a problem. The relationship is symbiotic, they cannot exist without each other.

When did problems become… a problem? How did the word carry such a negative connotation? I’ve thought of problem-making as a form of opportunity – because the problem exists or is created, there will more-than-likely be someone devoted to finding the solution. Without the problem, the need for a solution wouldn’t exist.

The entire thought about problems came to me one day when I was thinking about outrageous ideas for a novel (which I would never write due to my lack of ability to properly describe scenarios). I thought about having a villain as the main character. Following that, I thought about the cruelest and evilest villain possible, and came up with a person who had the ability to solve any kind of problem without regard to the possible consequences of the solution.

At first, the reader would have the impression that this was a wonderful gift to have – solving any kind of problem, and that the main character is this righteous person who wants to help the world. However, as the main character begins to solve every kind of problem, s/he fails to realize the consequences of the act – people begin to lose meaning within their lives because the problems they lived and existed to solve, were no longer available.

What happens is the world sees an increase in suicide rates. I know someone will call plot hole because those people can find other problems to work with. No. The main character is out to solve EVERYTHING, and is able to do it with extreme ease. The world populace deems the main character as a threat to humanity, and the story ends with his him solving this problem with his own death. I never decided what happened to the world after that, but I’d imagine anarchy as if the idea is to promote problem generation as a ‘solution’, then the world would have to turn onto itself for people to strive to find solutions.

But that brings me to the question, why would a problem have to be chaotic, negative, so consequential?

That’s why I’m inclined to say that I’m a problem creator. There’s too many problem solvers out there already. I want to be the one that creates problems so opportunity lends itself to another’s future.

Or, I could be wrong completely. I recall situations where I have to solve a problem, and after solving that problem, there is no additional problems generated from the resolution. Maybe its kind of like division and multiplication:

Problem x Solution = Problem_New

Solution / Problem = undefined (in certain cases)

Facelist Improved: Facebook Autocompletion JQuery Plugin

I was evaluating all the available JQuery plugins to get a Facebook-like contact autocompletion functionality, and deemed the one by Ian Tearle to be the most complete available. However, most complete does not mean fully done, or bug free, and I learned this the hard way doing an initial implementation on a project I’m doing at work.

Spending all day with the lovely Firebug debugger and testing in both Firefox 3 and IE7, I can say I have it working the way I need it to. My version fixes the following issues:

- Not all results that are returned are displayed

- Duplicate contacts can be inserted

- When searching, the user is not notified that a search is being performed

- The ID was being displayed with the search results, this has now been removed

There is still one lingering issue: the values stored in the hidden field… the plugin needs the trailing comma at the end of the values in order to properly perform backspace deletion. It’s really a minor issue as long as you remember in your code that parses the value, that when exploding the commas, you’ll have to ignore the final array row as it will be blank or null.

Demo

Download

I do not provide any support for my modified plugin, but feel free to leave comments if you hit bugs, and if I have time, I will fix them. I’m also hoping Ian will accept my changes so that I can just do a link to his page for the fix.

Ian Tearle’s Page for the Plugin

Edit: Yay, Ian accepted the changes, and they are now updated on the JQuery Plugin project page! I’ve updated my download link to point to the page.

Convert Oracle Date to UNIX Time

Oracle unfortunately does not have a UNIX conversion function. However, you can do it mathematically in SQL. I’ve written a function in PHP that will convert a date field to a UNIX timestamp in Oracle.

/**
* Inserts the SQL necessary to format a date to a UNIX time in Oracle.
* @param string Field name to convert.
* @param string (Optional) Field Alias
* @return string The SQL necessary to convert to UNIX time.
*/
function toUnixTime($field, $fieldAlias=null){
if($fieldAlias== null){
$fieldAlias= $field;
}

return "(ROUND((".$field." - to_date('01-01-1970', 'DD-MM-YYYY')) * (86400) + (28800)) ".$fieldAlias;
}

Usage:

$sql = “SELECT “.toUnixTime(“created”).” FROM users”;

or

//CreatedOn alias
$sql = “SELECT “.toUnixTime(“created”, “createdOn”).” FROM users”;

Oracle 10g Grid Agent Woes (+Solution!)

I started to use the Oracle 10g Grid at work, and have been configuring Agents for our various Oracle servers so I would have a central location to monitor them, as well as eventually attempt replication.

The BIGGEST problem I’ve had was trying to get the agents to talk to the 10g Grid Enterprise Manager server. I would do something like this:

> emctl upload

And get something like this:

“EMD upload error: uploadXMLFiles skipped :: OMS version not checked yet”

I’ve read so many different forums on how to handle this error, but none of them solved my problem.

What was going on was the hostname for the 10g grid upload read something like:

Agent URL         : https://some.internal.domain.in:3872/emd/main/
Repository URL    : https://some2.internal.domain.in:1159/em/upload/

The funny thing was, if I went to the Repo URL on the actual server, I get a nice page that says it works, but if I tried it on the server with the agent installed, I wouldn’t get anything at all. After around 13 hours of fiddling around with this, looking at emagent.trc, listener.ora, emd.properties, trying emctl clearstate agent, securing the agent, unsecuring the agent, etc, I found out what was up:

For some reason that fully qualified domain name does not register with the other servers in the network. What I did was in my hosts file, I added a definition for the Repo URL and the IP it went to.

After that, I got:

> emctl upload

EMD upload completed successfully

You don’t know how happy I am right now.

Also, just an FYI: 10g x86 does not install well on a Windows x64 edition. What happens is the entire server hangs on a file in the %agent%/bin directory called fix_4198214.exe. During the installation phase of the agent (or the Grid), when that directory has that file copied over, just stop the installation (does not actually stop, but pauses it), and rename the file to something else. When the installer encounters the file, it’ll give an error message. Ignore it and you can happily proceed with the install :D

Working iCarousel Javascript Implementation

My work wanted an image carousel implemented into the website, and I could barely find any decent carousel implementations out there until I came across iCarousel by Fabio Zendhi Nagao.

I loved the examples on his website of how to use and implement it, but unfortunately the code examples do not work at all. Reading the comments section of the website, I came to realize that it’s not just me who can’t get this stuff to work the first time.

Anyways, after spending a few hours tinkering with it, I finally figured out how to do the horizontal example, and I’m placing a working package here so you can also download it if you’d like:

http://journal.suteki.nu/wp-content/uploads/2008/05/icarouselh.zip

Edit: I am not taking any requests to fix any part of iCarousel. Fabio’s scripts are completely broken; he should just take down his page because there is no indication that he cares about fixing his script at all.

Oracle, Propel 1.3 and Dates

It took me awhile to figure out how to parse an Oracle date type with Propel 1.3. The easiest way to do it would be this:

define(“DATE_FORMAT”, ‘M j, Y g:i a’);

< ?=date(DATE_FORMAT, $event->getStartDate(null)->format(“U”));?>

Assume there is an Event that stores the date and time of when the event begins. The code above basically translates the Oracle date to UNIX time, where you can then use the PHP date function to parse it out.

CLOB and PDO, specifically Oracle

So, my new job has me working with Oracle which is in my book, a way different beast compared to mySQL. Everything I know about databases from mySQL basically had to be re-tooled for Oracle.

I spent around 3 hours trying to figure out how to work in a set of prepared statements (one of which returned a value) in PHP PDO, and thought I might share how I did this.

Okay, so I have one stored procedure and one function.

PROCEDURE RETURN_CLOB(:CLOB_ID, :CLOB_OUT IN CLOB) — All this does was search a table with key/ CLOB value pairs. It basically selected a row with the :CLOB_ID, and :CLOB_OUT returned the CLOB value.

FUNCTION CONVERT_CLOB_TO_VARCHAR2(:CLOB_OUT IN CLOB) RETURN VARCHAR2 — this was a function definition that took in the :CLOB_OUT from RETURN_CLOB and spit it out as a VARCHAR2

Note that I did not write these two procedures, it’s something out of a package I used at work, but was having trouble trying to get them to work in PDO.

I’m using PHP Propel 1.3, but you can easily rewrite this in standard PDO calls, since the $con object is actually a combination of the PDO class and PDO Statement class.
Here’s the code:

$con = Propel::getConnection(‘…’); //All this does is just use PDO to create a connection
/*

The call below is the same as PDOStatement::prepare(). I’ve condensed the following into one line:

DECLARE

CLOB_Output CLOB;

BEGIN

PackageName.RETURN_CLOB( :Clob_Id, CLOB_Output );

:Str := PackageName.CONVERT_CLOB_TO_VARCHAR2( CLOB_Output );

END;

//So the declare part is to define an internal variable in Oracle, which I’ve named CLOB_Output. It is of type CLOB. The Begin … End block processes the statements in one container.

1. Return_CLOB returns the CLOB data in CLOB_Output

2. Call CONVERT_BLOB_TO_VARCHAR2 with CLOB_Output

3. The converted CLOB is returned into :Str
*/
$stmt = $con->prepare(“DECLARE CLOB_Output CLOB; BEGIN PackageName.RETURN_CLOB( :Clob_Id, CLOB_Output ); :Str := PackageName.CONVERT_CLOB_TO_VARCHAR2( CLOB_Output ); END;”);

$m = “”;

$stmt->bindValue(‘:Clob_Id’, ‘Clob_Id_goes_here’, PDO::PARAM_INT);

$stmt->bindParam(‘:Str’, $m, PDO::PARAM_STR, 32767); //replace 32767 with the size of the VARCHAR2 that you are returning from CONVERT_BLOB_TO_VARCHAR2

$stmt->execute();
//$m now contains the CLOB as a VARCHAR2!
Phew! Anyways, I’m going to say that Propel 1.3 isn’t ready for Oracle yet, and neither is PHP_PDO for Oracle. There’s too many hassles and tricks I had to use to do the things I needed in Oracle, and I’m going to recommend people continue to use the OCI8 extension.

Connecting the Dots

There were notably three portions to Steve Jobs’ commencement speech at Stanford: connecting the dots, love and loss, and the notion of death.

For many years, many associates and family members have questioned my goal of completing five majors in community college. Their arguments were that it would take forever to accomplish, and that I was wasting time by staying in school for so long. To be honest, at the time, I didn’t see how my majors would mix together, but I knew I loved learning from those subjects.

When I found myself as a software engineer at NASA, my boss noticed I had a wide away of interests and focuses, and she was instrumental in connecting my own dots together. Although my primary job at NASA was to develop web applications, I soon found myself using my psychology, sociology, and social sciences knowledge running a lab part-time where we studied 3D cockpit displays for 747 airplanes.

The concept of human factors or human computer interaction (which tied parts of my CS and psychology knowledge together) came later when my boss urged me to attend a seminar presented by cognitive scientist Don Norman, who talked about product design, and emotions – the very core of what I was interested in, but never fully realized it until then.

The dots were beginning to connect, but all of them came together to draw a picture when I reached Stanford, working in the psychiatry labs doing emotions research. I found my CS knowledge was extremely handy in designing programs to analyze and collect data, and that my psychology and sociology knowledge bridged me to researchers and the data they collected.

Even though NASA was paying me more at the time, I found myself really enjoying what I did at Stanford, and left NASA to continue on emotions research. I’ve been there for two years, and have recently left as an employee. However, I truly enjoyed what I’ve been doing there, that I’m a continuing volunteer. I love learning about emotions, and its relation to life itself.

I’ve never pictured myself living beyond thirty, but that’s probably because I’m too young right now. But, I always feel that my time is limited, that I need to change people, and the world NOW, as opposed to later. It’s why I’ve taken on many opportunities that have come to me, even in the times where it feels seemingly impossible to accomplish.

No one regrets my decisions anymore, and I don’t regret the awkward path I’ve taken in my life with respect to college. It’s true that I’ll probably graduate when I’m twenty-six or even twenty-seven, but my life feels fulfilling in many ways, and many people are genuinely interested in having me incorporated into their futures.

I’m frequently asked nowadays, “What do you plan to do in the future?”, and the honest answer is, “I don’t know.”

Those kinds of answers are often scary, but when I say this response, I say it with utmost confidence. I’m not afraid of not knowing my future, because I know even greater things lie in it; I can accomplish anything because I am up to discovering, and conditioning my mind for that future.

I know I’ll live beyond thirty, but I want to keep this mindset, because I want to live as if everyday was my last.

Acceptance

Okay, here’s another from good ol’ Yalom. Because Yalom is so awesome, I have created this very short, yet sweet song:

—————–

I love Yalom,
And Yalom loves me,
He gets me in-to
PSY-CHO-LO-GY

—————–

With that said, here’s a nice passage on acceptance:

“Group members’ acceptance of one another, though crucial in the group therapeutic process, may be quite slow to develop. Acceptance by others and self-acceptance are mutually dependent; not only is self-acceptance basically dependent on acceptance of others, but acceptance of others is fully possible only after the individual can accept himself. The members of a therapy group may experience considerable self-contempt and a deep contempt for others.

A manifestation of this may be seen in the patient’s initial refusal to join ‘a group of nuts’ or his reluctance to become closely involved in the group for fear of being sucked into a maelstrom of misery.

The importance of self acceptance for the acceptance of others has been demonstrated in research by Rubin, who studied fifty individuals before and after an intensive live-in two-week T-group laboratory and found that an increase in self-acceptance was significantly correlated with increased acceptance of others. These results are consonant with Fromm’s statement many years ago that only after one is able to love himself is he able to love others. I would add, however, that only after he has once been loved will he be able to love himself.” – p.55-56