Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

158 Part III — Google Map Hacks


Listing 9-6:Creating the Table Structure

#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect( ‘dbi:mysql:database=mapsbookex;host=db.maps.mcslp.com’,
‘mapsbookex’,
‘examples’,
);

if (defined($dbh))
{
$dbh->do(‘create table ch09_simple ;
(lat float,lng float,title varchar(80))’);
}
else
{
die “Couldn’t open connection to database\n”;
}

Converting existing data and inserting that information into the database is a common task.
Listing 9-7 is a script that translates a colon-separated version of the restaurant information
(in title, longitude, latitude order) into the database and table that you created in Listing 9-6.

Listing 9-7:Populating the Database with Information

#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect( ‘dbi:mysql:database=mapsbookex;host=db.maps.mcslp.com’,
‘mapsbookex’,
‘examples’,
);

if (!defined($dbh))
{
die “Couldn’t open connection to database\n”;
}

open(DATA,$ARGV[0]) or die “Couldn’t open file; did you forget it?”;

my $counter = 0;

while(<DATA>)
{
chomp;
Free download pdf