Wednesday, August 29, 2012

Blogging from 3G tethered connection on E51

The connection is pretty fast to work on...I will explain how I tethered my E51 in another article! PS: This is not a bullshit post - since I work from home and my internet connection screwed up, I had to resort to 3G!

Monday, August 27, 2012

MySQL Error while dropping tables: Cannot delete or update a parent row: a foreign key constraint fails

If you know what you are doing and you still want to drop a table containing foreign constraints, then you need to follow this procedure:

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE t1;
SET FOREIGN KEY_CHECKS=1;

Perl DBI - Connecting Perl to MySQL database

Knowing how to connect Perl with a database is imperative and very useful. Luckily there is a Perl module called DBI which makes our things easier. In this article I will demonstrate how to write a Perl program to connect to a MySQL database and fetch some rows. Consider the following code:


#!/usr/bin/perl

use strict;
use warnings;
use DBI;

# Main program
main();

# Subroutines
sub main {
  
  my $dbn="dbi:mysql:testdb:localhost:3306";
  my $dbh=DBI->connect($dbn, "user", "password") or die "Unable to connect: $DBI::errstr\n";
  my $select_query="SELECT * FROM t1 ORDER BY id ASC";
  my $select_h = $dbh->prepare($select_query) or die "Unable to prepare: $DBI::errstr\n";
  $select_h->execute() or die "Unable to execute: $DBI::errstr\n";
  while ( my @data = $select_h->fetchrow_array()) {
    print "Column 1: $data[0], Column 2: $data[1]\n";
  }
  $select_h->finish();  
  $dbh->disconnect;
}

1. Before starting make sure you type the use DBI statement.
2. Define the database handle as follows: dbi:mysql:db_name:db_host:db_port:db_user:db_password.
3. Connect to instance using DBI->connect().
4. Prepare statement using $db_handle->prepare().
5. Execute statement using execute().
6. Fetch rows by looping the fetchrow_array() function.
7. Finish the statement and Disconnect from handle.

Sunday, August 26, 2012

Perl parallel tasks using fork

I am working on a Perl application which needs to do repetitive tasks. As such I am using the mighty fork() - actually to make things easier for myself, I am using the Parallel::ForkManager module. The following piece of code spawns 5 children running as parallel processes:

#!/usr/bin/perl

use strict;
use warnings;
use Parallel::ForkManager;

main();

sub main {

  my $pm = new Parallel::ForkManager(5);

  for (my $i=0; $i <= 10; $i++) {
    my $pid = $pm->start and next;
    print "Spawned process $$\n";
    $pm->finish;
  }

  $pm->wait_all_children;
}


Note: The child PID returns 0 inside parent but we can use the $$ special variable to get the PID of the child.

Perl thread vs fork

After an extensive research on parallel processing with Perl I came to the conclusion when to use threads and when to use forks.

1. If you have a set of repetitive tasks, use forks.
2. If you require lots of communication between processes, use threads.

Remember that threads are heavier than forks but on the other hand forks are too complex to handle inter-process communication. Sometimes you may just need to share parent variables to children and if that's enough, than go for forks.

Saturday, August 25, 2012

From gedit to geany

I want to hit myself for not discovering geany earlier. This is a fantastic lightweight IDE/text-editor for Linux. Do yourselves a favor and install it if you haven't done so. For those familiar with Notepad++ on Windows, this is practically similar with more features such as a built-in terminal, support for projects, color picker, etc...

Friday, August 24, 2012

WhatsApp and Nokia Symbian phones

Many Nokia users (including myself) used to hate WhatsApp because it drains the mobile's battery almost after a couple of hours. This was due to a bug which is now fixed. Go ahead and download the latest version from their website.

Thursday, August 23, 2012

This blog got rejected from Adsense 5 times

... and I don't give a fuck anymore ;)

Introduction to pt-table-sync and pt-table-checksum

In this post I would like to introduce you to a couple of must-have tools from Percona which enable you to validate the table contents between two MySQL databases. This is especially useful in replication setups and as a way to ensure that both master and slave have the exact same data, and if not, to sync them accordingly.

pt-table-checksum: This tool can be used to detect any differences between master and slave, however it is not able to tell which are the differences - only where are they.
Usage example: pt-table-checksum -uusername -ppassword --create-replicate-table --replicate percona.checksums -h master1

pt-table-sync: This tool can be used both to detect differences between master and slave and to fix the differences. Unlike pt-table-checksum, this tool is able to actually understand the difference between the master and slave.
Usage example: pt-table-sync -uusername -ppassword --print --sync-to-master --replicate percona.checksums -h slave1 --databases database1, database2

One common question is "why do you normally run pt-table-checksum before pt-table-sync?". The answer is simply so pt-table-sync does less work! In fact, pt-table-checksum will record in a table (percona.checksums) where there are difference (but it doesn't know what are the differences) and pt-table-sync will analyze and correct only these chunks.

For example: If you have a table with 1M rows and you run pt-table-checksum using chunks of 1k rows each, pt-table-checksum detects that in 3 chunks there are differences. When you then run pt-table-sync it will analyze and correct only these 3 chunks (3k rows) instead of the whole table.

Wednesday, August 22, 2012

What sort of music you listen when you work?

I personally like putting my media player in the background streaming classic rock, although at the moment I am listening to the whole Beatles discography. What about you?

Merge multiple Google Calendars

So yesterday I was complaining that my old Nokia E51 was only able to sync one calendar through the Mail for Exchange tool. This is a biggie for me as I have many other shared calendars which I use. Apparently there is no out of the box solution which would allow us to automatically copy events from one calendar to another and keep them sort of in sync.

The current solution to this quite common problem is to export the calendar as ical and then reimport it to the target calendar.

Step 1 - Export to ical:
- In the google calendar list go to the left hand side and from the pull down menu of the source calendar, click on Calendar Settings
- scroll down->Private Address->right click the ICAL button and save it to your desktop

Step 2- Import to your calendar:
- In the google calendar list go to the left hand side and from the pull down menu of the Other Calendars parent menu, click on Import Calendar. Choose the destination calendar and you're ready.

Tuesday, August 21, 2012

Nokia e51 internet connectivity

I connect to internet on my E51 using one of these methods:

1. Wifi - this mobile has one hell of a signal strength, especially if you disable the power saving setting in the Access Point of your wifi profile (settings->connection->access points->your wifi profile->advanced settings). To save some battery, once i connect to the wifi, I switch off the scanning from the front screen.

2. UMTS/3G/HSDPA - this is also known as packet data and which costs you money per MB downloaded/uploaded. I normally disable this when I am connected to wifi (just in case!) by going on settings->connection->packet data->high speed packet access->Disabled.


Display multiple variables in mysql

Sometimes you might need to gather a handful of variables in one statement. This can be done by using the following syntax:


james@ebony ~ $ mysql -uroot -p ebonydb -e "show variables where 
variable_name like 'long_query_time' 
or variable_name like 'read_only'"
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
| read_only       | OFF       |
+-----------------+-----------+

Bye Bye Iphone 3GS, back to Nokia E51

Yesterday's midnight swim was remarkably inaugurated by an accidental iPhone dive which I left in the swimming trunks pocket. Apparently this is a very common fate for these mobile phones.

I had no other option to go back to my old Nokia E51, which thinking about it is a really good phone for work. Did the usual stuff - google sync, whatsapp, etc. Only one thing I was disappointed with - Nokia's "Mail for Exchange", which by the way integrates seamlessly with Google Mail and Google Calendar, supports only one calendar...so try to work around with that.

Other than that I discovered some features which are quite unique - enable power thresholds for wifi signals being the one which caught my attention.

Anyways, due to its long time in the box, I need a new battery, so I ordered one from flea bay - only after I made sure this could be a suitable phone to work with in at least the next 6 months. My next project is to figure out how to tether it.

Friday, August 03, 2012

Change your Youtube login email

Today I managed to reclaim back my original youtube channel. I had to wait for Youtube monkeys for more than a year to be able to give us the option of changing the login email address. The process is now very simple - go to the bottom of the account settings screen and choose to unlink your google account from the youtube account:


Now you are able to link your account with your new email address.

Thursday, August 02, 2012

Add workspaces in Linux Mint Cinnamon

Although the Cinnamon desktop environment is available to every Linux distro, it is much more widespread and popular among the Linux Mint users. One of the most common questions I get asked is how to add or remove workspaces. We know that by default we get two workspaces, unlike Gnome which sometimes comes with four.

The solution is simple - move the mouse to the top left corner of the screen and you will get to the 'expo' mode, made popular by the mac community:


You can add a new workspace by clicking on the righthand bar (it has the plus sign on it). You can remove existing workspaces but hovering over them and clicking on the top right corner, as shown in the image above. If you hover the mouse again to the top left corner of the screen, you will go back to your desktop. You can hover to the other workspaces by pressing ctrl-alt and the left or right arrow keys.