Wednesday, March 11, 2009

Rename multiple tables in MySQL database

Having to rename multiple tables with a similar pattern (prefix, suffix, ...) by replacing the recurring pattern, isn't an easy task, because there is no simple function or statement for doing it, not as far as I know.
Renaming tables like 'table_1234' and 'table_3456' to 'new_table_1234' and 'new_table_3456', is pretty straightforward in SQL:


RENAME TABLE `database`.`table_1234`
TO `database`.`new_table_1234`;
RENAME TABLE `database`.`table_3456`
TO `database`.`new_table_3456`;

or in one SQL statement :

RENAME TABLE
`database`.`table_1234` TO `database`.`new_table_1234`,
`database`.`table_3456` TO `database`.`new_table_3456`;

But when renaming a lot of tables, this method becomes tedious and time consuming.

This PHP script automates the renaming of multiple tables in a MySQL database. It lists all tables in a MySQL database, which contain a defined string pattern. The script creates and executes a series of SQL statements, which rename the table by replacing the search pattern in the original table name with another pattern in the new table name.

This script can easily be modified to rename multiple databases, or when stripping the original pattern (f.e. a prefix) and adding a new pattern (f.e. a suffix) is needed.

<?php
$db_server = "localhost"; // hostname MySQL server
$db_username = "username"; // username MySQL server
$db_password = "password"; // password MySQL server
$db_name = "database"; // database name

$pattern = "pattern_"; // search string
$new_pattern = "new_pattern_"; // replacement string,
// can be empty

// login to MySQL server
$link = mysql_connect( $db_server, $db_username, $db_password);

if (!$link)
{
die('Could not connect: ' . mysql_error());
}

// list all tables in the database containing the search pattern
$sql = "SHOW TABLES FROM `" . $db_name . "`";
$sql .= " LIKE '%" . $pattern . "%'";

$result = mysql_query ( $sql, $link );
if (!$result)
{
die("Invalid query: " . mysql_error( $link ));
}

$renamed = 0;
$failed = 0;

while ( $row = mysql_fetch_array ($result) )
{
// rename every table by replacing the search pattern
// with a new pattern
$table_name = $row[0];
$new_table_name = str_replace ( $pattern, $new_pattern, $table_name);

$sql = "RENAME TABLE `" . $db_name . "`.`" . $table_name . "`";
$sql .= " TO `" . $db_name . "`.`" . $new_table_name . "`";

$result_rename = mysql_query ( $sql, $link );
if ($result_rename)
{
echo "Table `" . $table_name . "` renamed to :`";
echo $new_table_name . "`.\n";
$renamed++;
}
else
{
// notify when the renaming failed and show reason why
echo "Renaming of table `" . $table_name . "` has failed: ";
echo mysql_error( $link ) . "\n";
$failed++;
}
}

echo $renamed . " tables were renamed, " . $failed . " failed.\n";

// close connection to MySQL server
mysql_close( $link );
?>

Syntax Highlighting in Blogger

I've added some Javascript to the template of my blog to get Syntax Highlighting working. This means source code is formatted so it is easier to read. If it works allright, you should see an example of some highlighted php-code below. (The source code is not formatted/coloured when reading this post through an RSS-feed.)


<?php
/* Sample php code
This code doesn't do anything at all.
It just contains some php elements that should be formatted
and coloured.
*/

$some_variable = "some_string";

// Some comment
$result = some_function( $some_variable );

if (!$result)
{
die("It doesn't work!");
}

$some_array = new array();

$some_array[0] = $result;
?>

Saturday, February 07, 2009

Green Plug Universal Power Adaptor

How many electronic devices do you have? A laptop, cell phone, MP3 player, external hard disc, GPS, ...
And each of them has its own adaptor, which can only be used with a specific device and isn't compatible with your other devices.
So you end up with several different adapters powering your devices, which results in a lot of cables that can get entangled, and which take up a lot of power plugs.
And if an adapter breaks, you have to buy a spare one from the original manufacturer, which costs a lot.

Wouldn't it be nice if there was a universal adapter for all your electronic devices?
You would only have to take one adaptor with you, to charge your cell phone, MP3 player and laptop. If a friend or colleague forgot to take her or his adapter, he or she could easily use yours, even if he or she uses a laptop of a different brand.

Actually, there is an initiative to standardize power adaptors :
Green Plug Universal Power Adaptor.


GreenPlug Universal Adapter

Monday, January 12, 2009

Enabling wireless network on an Acer Aspire One in Ubuntu

I just installed Kubuntu 8.10 on an Acer Aspire One. Everything seems to work fine, but the wireless network doesn't seem to work.
Luckily I found this HOWTO on enabling the wifi.

Friday, November 21, 2008

Social Web

Social networking websites are becoming more and more popular, and it's number is growing. Some websites provide a unique service, while other websites seem to be more of the same.
Some of your friends join one website while others join another, and both of them are requesting to join them on their website of choice.

After a while you have an account with several of those websites, and you start to notice you are providing the same information over and over again, on every of those websites : personal details, interests, schools you attended, employers you worked for.
And you have to (re)connect to your friends on every of those websites.
Not to mention when something changes, for instance when you change jobs, you have to change it on every website and hope you don't forget any of them.

Wouldn't it be convenient if all of the information you provide on those websites, can be shared between those websites? If you want to change something, you can do it in one place and all other social networking websites where you have an account adopt these changes automatically? If you add a friend on one website, and that friend has an account on another website where you have an account too, that this friend is added there as well?

I'm not the only one asking the same questions. And it seems some effort is put into creating a Social Web, where this kind of (personal) data can be shared between (social networking) websites in a open, easy and secure way.
These are a few of the initiatives :

A recent presentation (June 2008) was held with an overview of these efforts and what the future will bring. Unfortunately, these efforts, for now, are quite theoretical and about creating standards based upon existing web standards. Several protocols are proposed (FOAF, XFN, hCard, GRDDL, RDF, OpenID, OAuth, REST, ...) to be used in this new standards.

No real usable application is available at the moment, not as far as I know, but the big players (MySpace, Facebook, Google, Twitter, Netlog, ...) are backing some of these initiatives.

Thursday, October 09, 2008

Noisy fan

I noticed a whining noise, coming out of my computer, and it turned out to be caused by the fan on my graphics card. It is about 3 years old, so the fan starts to wear and will soon stop turning, which could be disastrous for the GPU on the graphics card.

So I started looking for a replacement fan on the producer's website and found this :

Noisy Fan or Fan Failure

The fans on ATI graphics cards are not user replaceable parts. To avoid any complications due to any possible damage that may have been caused by the fan's failure or degraded functionality, the entire card should be replaced. If the card is currently under warranty and built by ATI, you can fill out and submit the online warranty request form at http://www.ati.amd.com/rma.


As I mentioned, my graphics card, a ATI Radeon 9600 XT 128MB, is 3 years old, so past warranty, but it still works fine. It seems I'll have to find a replacement fan, without help from ATI. I'm sure I'll find one, but I'm a bit upset ATI provides such a lousy service.

I can understand, they want you to buy a completely new and costly graphics card that's faster and better, but maybe if you read something like this, a customer might consider buying a graphics card of another supplier next time. One that does provide spare fans. ;)

Tuesday, September 23, 2008

RIP Mark Hoekstra

Mark Hoekstra, a young Dutch guy, passed away last week, after suffering a heart attack. He was only 34.

He hosted the geektechnique.org website, informing the world of his ongoing projects and hacking together of electronic gadgets.

His death is a great loss for the Open Source and hacking community.

RIP Mark Hoekstra, you will be missed.