Hi All,
I had a MySql DB and i needed to read the info from a certain table and store it into an Access database. So i wrote a script to do this and it worked so well that i thought i'll share it with you guys. You can also play around with it to have it work the other way around. Enjoy!
-Kiko
#!/perl/bin/perl -w
use DBI;
use Win32::ODBC;
use User::pwent;
# Global Variables
my @statement; # Array of sql statements that will be used for Acce
+ss
my @row; # Array of table rows read from MySql
my @message; # Array of error messages used for debugging Access
# MySQL Connection
my $dbh = DBI->connect('DBI:mysql:DBName:localhost',
'UserName', 'Password',
{ RaiseError => 1, AutoCommit => 1 });
# Access Connection
my $connection=new Win32::ODBC("DSN=;UID=;PWD=;");
unless ($connection) {
push @message,"Unable to connect to database!";
}
push @message,$connection->Error;
# Create the sql statement for MySql and execute it
$sth = $dbh->prepare("SELECT url,title,keywords from addresses");
$sth->execute;
while ((@row) = $sth->fetchrow_array) {
# This is were we grab all the table rows from MySql and store the
+m into
# an array to be inserted into Access.
push @statement,"INSERT INTO keywords (url, title, keywords) VALUE
+S ('$row[0]', '$row[1]', '$row[2]')";
}
# Execute sql statments and if there is an error report it
foreach (@statement){
$connection->Sql($_);
if ($connection->Error()){
print "\nERROR: \nThe SQL statement:</b>\n $_ \n\n<b>caused th
+e error:</b>\n\n ",$connection->Error();
die();
}
push @message,$connection->Error();
}
# Close the Access DB Connection
$connection->Close();
# Close the MySql DB connection
$sth->finish;
$dbh->disconnect;