Archive for December, 2009

Import DBF Files To MySQL Using PHP

Thursday, December 31st, 2009

I recently had a task to do for a friend of mine to migrate Visual FoxPro data into a mysql database.

As a programmer that doesn’t like to re-invent the wheel, I tried searching through google, but was only partially successful in getting code to insert dbf data into mysql (it was public domain code, but only had code to insert data into a table that was created earlier).

This is the code modified to work with any dbf file – it creates the table, then inserts the dbf data into the created table. The code was further modified to strip whitespaces from the text values (FoxPro/DBase pads text with spaces up to the length of the field). You may want to change this further, since this is a quick and dirty code for the sole purpose of importing dbf tables. This works with my rpms at the repository.


/*
* Part 2 code modified by Clint Christopher Canada from different public domain sources
* Part 1 code created by Clint Christopher Canada. BSD licensed.
*/

// This is Part I of the code
$tbl = "table to create";
$db_uname = 'mysql username';
$db_passwd = 'mysql password';
$db = 'database';
$conn = mysql_pconnect('localhost',$db_uname, $db_passwd);

// Path to dbase file
$db_path = ".DBF";

// Open dbase file
$dbh = dbase_open($db_path, 0)
or die("Error! Could not open dbase database file '$db_path'.");

// Get column information
$column_info = dbase_get_header_info($dbh);

// Display information
//print_r($column_info);

$line = array();

foreach($column_info as $col)
{
switch($col['type'])
{
case 'character':
$line[]= "`$col[name]` VARCHAR( $col[length] )";
break;
case 'number':
$line[]= "`$col[name]` FLOAT";
break;
case 'boolean':
$line[]= "`$col[name]` BOOL";
break;
case 'date':
$line[]= "`$col[name]` DATE";
break;
case 'memo':
$line[]= "`$col[name]` TEXT";
break;
}
}
$str = implode(",",$line);
$sql = "CREATE TABLE `$tbl` ( $str );";

mysql_select_db($db,$conn);

mysql_query($sql,$conn);
set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.

// This is part 2 of the code

import_dbf($db, $tbl, $db_path);

function import_dbf($db, $table, $dbf_file)
{
global $conn;
if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
$num_rec = dbase_numrecords($dbf);
$num_fields = dbase_numfields($dbf);
$fields = array();

for ($i=1; $i<=$num_rec; $i++){
$row = @dbase_get_record_with_names($dbf,$i);
$q = "insert into $db.$table values (";
foreach ($row as $key => $val){
if ($key == 'deleted'){ continue; }
$q .= "'" . addslashes(trim($val)) . "',"; // Code modified to trim out whitespaces
}
if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
$q = substr($q, 0, -1);
$q .= ')';
//if the query failed - go ahead and print a bunch of debug info
if (!$result = mysql_query($q, $conn)){
print (mysql_error() . " SQL: $q
\n");
print (substr_count($q, ',') + 1) . " Fields total.

";
$problem_q = explode(',', $q);
$q1 = "desc $db.$table";
$result1 = mysql_query($q1, $conn);
$columns = array();
$i = 1;
while ($row1 = mysql_fetch_assoc($result1)){
$columns[$i] = $row1['Field'];
$i++;
}
$i = 1;
foreach ($problem_q as $pq){
print "$i column: {$columns[$i]} data: $pq
\n";
$i++;
}
die();
}
}
}

?>

Binary Clock – A Simple Java Program For Mobile Phones

Thursday, December 17th, 2009

Here’s an interesting take into how to make an open source program. This should make a good case study on how to plan out and develop a program for distribution to the open source community.

Presenting Binary Clock – a GPLed java program which shows the time in binary. This is a simple java midlet made for nokia phones (from what I’ve heard, it runs on samsung phones too).

Anyways, here are the steps (illustrated):

1. Decide on what you want to do with the program.  You can then make a mock drawing on a piece of paper on how the program will respond like what is shown below:

BCCanvas

2. You would also have to determine the target audience for your program.  In this case, we target mobile phones, mainly nokia phones.  To do this, you would have to have appropriate tools for the job ready.  Shown below are the tools used to make the program (testing and design).  Note that all these tools run in linux (although there are windows versions available, except for Fireworks which is a windows program); Fireworks was run in wine (yes, it does run, for those who are curious).

Bildschirmfoto

Bildschirmfoto-1

Bildschirmfoto-2

Bildschirmfoto-Macromedia Fireworks MX

The final design from the Fireworks layout is below:

canvas-background

3. Since we have tested this in our emulators/sdk, it is now time to test it by ourselves with our available hardware.

Click here To Watch Video
Click to Watch!

Click here To Watch Video
Click to Watch!

Click here To Watch Video
Click to Watch!

4. When you feel that it is more or less ready to be distributed, you announce it to the world, and allow other people to modify and enhance our code.

Many thanks to Daniel Rindt of Visetics (http://www.visetics.com) for sharing the development workflow and the actual program with me.  To get the actual source code, please get it here.

Google, Facebook, Twitter and Privacy

Friday, December 11th, 2009

A few days ago, I received a wall post from the Facebook blog, that they have teamed up with Google Real Time Search. I did a little research on what that truly meant and found this post here on the official Google blog.

In a nutshell, this is what is happening. Google is getting posts and status updates from many networking sites and allowing them to be shown in the search results. How are they gonna do that? I got my answer within a few days.

Facebook had a change in their privacy settings. The time that you log into facebook, you will be notified of the privacy settings page and would suggest you that you change your settings to their recommended one – one that is seen by practically everyone.

Now, I am no privacy freak. Far from that. What I am concerned about is that by allowing your profile to be shown to all, a person will risk being used or taken advantage of by unscrupulous individuals in the net.

I’ve had my cellphone number been sent text messages by people I do not know about some nice deal which was clearly a scam. I’ve had a rise in spam messages when I placed my email in a post or comment in a blog.

Having said that, I strongly agree with Bruce Schneier, a well known computer security specialist, that:

For if we are observed in all matters, we are constantly under threat of correction, judgment, criticism, even plagiarism of our own uniqueness. We become children, fettered under watchful eyes, constantly fearful that — either now or in the uncertain future — patterns we leave behind will be brought back to implicate us, by whatever authority has now become focused upon our once-private and innocent acts. We lose our individuality, because everything we do is observable and recordable.

in his blog as a rebuttal to Eric Schmitt, CEO of Google, who said a dangerous statement to Maria Bartiromo during CNBC’s big Google special, that “If you have something that you don’t want anyone to know, maybe you shouldn’t be doing it in the first place.”

Think about it. You don’t hide things from people you don’t know because you did something bad. It is more likely that you hide things from people you are not likely to trust because they would like to do bad things to you.

FFMPEG Settings for Optimal Flash H264 Encoding

Wednesday, December 9th, 2009

H264 video has increasingly been used in flash videos recently.

The challenge of most people have been to find out a sweet spot for encoding h264 flash videos which delivers near original quality but with significantly less file size than the original.

Here’s a code snippet that you can use for ffmpeg as shown below:

ffmpeg -i /yourfile.mov -y -f mp4 -vcodec libx264 -crf 28 -threads 0 -flags +loop -cmp +chroma -deblockalpha -1 -deblockbeta -1 -refs 3 -bf 3 -coder 1 -me_method hex -me_range 18 -subq 7 -partitions +parti4x4+parti8x8+partp8x8+partb8x8 -g 320 -keyint_min 25 -level 41 -qmin 10 -qmax 51 -qcomp 0.7 -trellis 1 -sc_threshold 40 -i_qfactor 0.71 -flags2 +mixed_refs+dct8x8+wpred+bpyramid -padcolor 000000 -padtop 0 -padbottom 0 -padleft 0 -padright 0 -acodec libfaac -ab 80kb -ar 48000 -ac 2 outputfile.mp4

The resulting command line switch above can produce outstanding results. You can use the ffmpeg application available in my rpm repository.

See the resulting h264 file below (original 720p file is 397mb, the flash optimized file is 89.4mb):

Click here To Watch Video
Click to Watch!

Fixing Windows Live Error Code 800705b4

Friday, December 4th, 2009

On my free time, I like testing out and playing games.  This is because, as a wine applications maintainer in the past, I had to cross test between distributions (mainly PCLinuxOS, and sometimes slackware at the time) and new updates to wine (particularly before it reached version 1.0).  Unfortunately, as work went more demanding, I had to leave all these behind.

** Sigh **

I was even surprised when my name came up on the ubuntu forums one time (archived already of course), regarding wine:

They always say I’m from Canada, even if that’s my Last Name :) (Distrowatch got my name wrong too)

Back to the topic, I’m getting offcourse…

Anyways, as I’m mainly in windows for testing games now, I’ve noticed an error in my Vista box lately after I updated my Batman: Arkham Asylum game through Windows Live:  800705b4.  This happens when I log into Windows Live, after which, the game would totally become unresponsive and has to be shut down from Task Manager.

After looking for a solution (this was a very cryptic error message, and many users don’t know what to do with it), I found a solution which works for me: open up the command prompt in administrator mode and type in the commands:

netsh winsock reset

and

netsh int ip reset

and reboot your pc.

This should fix your Windows Live problems.  As a side note, I find this one to be the best of all the superhero games I’ve seen so far (good story, albeit not for young audiences – stays mostly true to the Dark Knight stories in comics, depth and actually lets you feel you’re Batman – LOL) – and I’ve played quite a number of Batman games for the NES and SNES and Genesis, and a very nice game to see graphically – it puts the Unreal game engine to good use (which is quite impressive because this game is not a First Person Shooter, which I’m not fond of).