Fixing mysqldump character-encoding in Vim

If you find yourself in a position where your mysqldump backup/restore process isn’t working, it’s worth checking for character-encoding issues – and the best way to do this is often to look at the SQL in your backup file.

To tell vim you prefer working in unicode, you may have added some settings to your .vimrc:

set encoding=utf-8 fenc=utf-8

That doesn’t get vim to treat existing files as UTF-8 though! Vim tries to figure-out the encoding itself, and may well get it wrong.

Looking at the mysqldump file, you could see garbage like:

One naïve approach

Something’s horribly wrong there. It looks like an encoding issue, and a quick :set fenc shows you whether vim opened the file in latin1 or utf-8.

You can force vim to re-open the file in utf-8 using:

:e ++enc=utf8 %

Hopefully, you now see:

One naïve approach

Now you know you’ve got unicode in your mysqldump, you can fix the restore process by a bit of search-and-replace on connection settings and table-creation statements. i.e you’re looking for lines like:

/*!40101 SET NAMES latin1 */;

…and…

ENGINE=MyISAM DEFAULT CHARSET=latin1;

Switch those from latin1 to utf8 and, fingers-crossed, you should be able to restore your db backup, and upgrade all your tables to utf8 in the process.

Leave the first comment

MySQL latin1 → utf8 (WordPress upgrade)

Spurred on by mass hacking, I’ve updated my old (version 2.0.3) WordPress install to something a little newer.

I decided to err on the side of caution and upgrade a copy of the live DB first – and I’m glad I did. I saw lots of problems with accents and symbols getting munged in the upgrade – well, that’s what I thought.

In reality, the symbols were being screwed in the backup-and-restore process. Words such as “naïve”, £-signs and various typographic quotes were obviously getting messed up through character-encoding issues.

After a bit of sleuthing, I found the output from mysqldump wasn’t valid. The problem was caused by a combination of default connection settings in a .my.cnf, and the fact that the old WordPress install was storing utf-8 characters inside a latin1 database.

My MySQL default settings are:


[client]
default-character-set=utf8

Because we’re pulling latin1 data over a utf-8 connection, MySQL starts doing character-set conversions, and screws up a bunch of text that it thinks is latin1, but in reality is actually already in utf-8.

The fix for the backup process was to override my default settings with latin1:


$ mysqldump --default-character-set=latin1 --opt -h db.example.com -u user -ppassword schema > db-backup-latin1-20080707.sql

That worked fine, and the next step was reloading it into the DB as utf-8. This required a little bit of string replacement using a command-line utility bundled with MySQL. If you’re going to do this yourself, watch what you type: I somehow typed in “lastin1″ halfway through and lost an hour or so trying to figure out what went wrong. Anyhow, here’s the command-line:


$ replace "CHARSET=latin1" "CHARSET=utf8" "SET NAMES latin1" "SET NAMES utf8" < db-backup-latin1-20080707.sql > db-backup-utf8-20080707.sql

You should now be able to blat / restore / overwrite your DB and ensure all tables are in the appropriate character set, ready for a smooth wordpress upgrade.


$ mysql --opt -h db.example.com -u user -ppassword schema < db-backup-utf8-20080707.sql

12 comments so far, add yours

In search of an Adobe Reader alternative on Mac

I’m looking for an alternative to Adobe Reader on OS X after discovering its “Find” function can’t find jack.

As I’m still running Tiger (OS X 10.4) I find Preview to be a tad lacking in usability – if only the ‘Maximize’ button maximized height and width, I’d be happy. But no, it’s a typical Mac-spastic application – maximize makes the application full-height, but doesn’t change the width. If you’re viewing a PDF using “Fit to Width”, this means the text-size stays exactly the same… which is never what I want.

If only Foxit Reader worked on Mac, I’d be using that (it’s a damn nippy and reliable piece of software on Windows.) But sadly it doesn’t – I even downloaded the Linux version, you know, just in case it worked on OS X (being a *nix ‘n all.)

So, I’m currently taking a look at PDF-XChange Viewer which comes in a couple of different versions. They provide a wee comparison chart to help you choose which version of their PDF viewer to download – but guess what format the chart’s provided in. You guessed it: PDF.

That doesn’t give me much confidence…

Updated 7th July 2008

It didn’t take long to realise PDF-XChange doesn’t even work on OS X. I’d been working my way down the “multi-platform” list of PDF viewers at wikipedia assuming “multi-platform” meant Windows and OS X – but it seems someone thinks PDF-XChange is multi-platform simply because it runs on more than one version of Windows.

Finally, I installed Skim from sourceforge. This open-source PDF reader works pretty well. Searching within documents is user-friendly – (by default) a pane on the left shows extracts matching the search term; click-to-navigate brings the document to the right page and highlights the search-term with both background-colour changes and by ringing the terms in red, making them easy to spot.

Leave the first comment

Amazon’s bizarre “Free HDMI cable” deal

Sony don’t include an HDMI cable with the UK’s PlayStation 3 console (40GB version). But, if you buy it from Amazon, they’ll give you an HDMI cable free – if you ask for it.

As Jacob Nielsen (usability expert) said: Amazon is no longer the role model for e-commerce design. I totally agree. I completely missed the opportunity to get a free HDMI cable, and I blame their UI.

It’s my own fault. I’ve become a bit lazy, and trust Amazon to do the right thing. I buy stuff from them quite regularly, and the only thing I really check is (a) the price, and (b) is it really being sold by Amazon (or one of their market-place sellers.)

I missed seeing the offer completely! As I write, Amazon are flogging the PS3 for £279. Right below it they show an HDMI cable as a “perfect partner” – to buy with the PS3 for £299.98 – i.e. it looks like the cable costs £19.99.

I may be a cheap-skate, but I wouldn’t spend £20 on a random cable – I’d either buy as cheap a no-name brand as possible (£5 or £10), or double-up and spend £40 on a brand I trust (QED or Ixos.)

Anyhow. It turns out that if you go for the £299.98 deal, Amazon take the £20 off at checkout time… you don’t even have to mail-in a rebate form. I find that a little misleading… on the one hand it’s great that they’re giving away a free HDMI cable – and on the other, they’re pretending it’s not.

I guess I’m just miffed because I didn’t read the small-print and completely missed the deal. I’m said to say I lost a little faith in Amazon today.

If they’d surprised me with the cable I’d be delighted, and shout their praises. As it is, I’m a little saddened, and feel I have to pore over every product page in future. My casual shopping days are over.

One comment so far, add another

Upgrade your new Macbook Pro’s memory yourself, and earn a free iPod (or two)

Apple are marking the launch of the iPhone by keeping all their US stores open from 6pm till midnight. I suspect the sales-process will be slow (since contracts need to be signed) and the stores will be full of people picking up random goods just to pass the time.

Not that this has anything to do with the iPhone, but if you’re going to buy a MacBook Pro with the full 4GB memory you have to make a decision:
1) MacBook Pro with 4GB memory installed by Apple
2) MacBook Pro with 4GB memory you installed, PLUS an 80GB iPod Video, PLUS an iPod Shuffle

It’s your choice. Installing memory is a piece of cake: pop the battery out and take off the memory cover (4 screws), replace old memory with new memory, then put the cover and battery back on. All in, it’s a couple of minutes.

So, the Maths: (with an ‘S’, because I’m English.)

UK Price US Price
Apple Upgrade Charge (2GB to 4GB) £480 $750
4GB memory (from Crucial) £180 $279
Saving £300 $471

That’s some juicy saving… Let’s buy some iPods:

UK Price US Price
Apple iPod Video 80GB £239 $350
Apple iPod Shuffle £49 $79
Total £288 $429
Remaining Savings £12 $42

Even after buying two iPods, you still have money left from your savings to buy drinks to celebrate.

Now I’m not going to moan about the rubbish exchange rate being used – I think we’re accustomed to being stiffed in Britain; but I will say something in Apple’s defence: they’re not alone. Dell also charge similarly extortionate prices for memory upgrades on their laptops. I’m not been writing about Dell for two reasons: (1) I’ve no idea if their laptop memory’s as easy to replace, and (2) they don’t make iPods.

And finally. Back to the iPhone. Instead of blowing your $470 savings on iPods, you could always use it for the majority payment of your iPhone (whether it be the $499 or $599 version.)

Good luck to those of you trying to get your grubby mitts on an iPhone tonight.

3 comments so far, add yours