#!/usr/bin/perl -w

#-- copy_db.pl

use DBI;
use strict;

my($dsn) = "DBI:mysql:nations:localhost";
my($username) = "quentar";
my($password) = "me42000";
my($dbh, $sth, $sth1);
my(@ary);
my($q);
my($code,$religion,$percent);

$dbh = DBI->connect($dsn,$username,$password) or die("no way");
print "connected\n";

$q = "DROP TABLE my_copy;";

$sth = $dbh->prepare ($q);
$sth->execute() or print("could not drop table\n");
print "droped table\n";

$q = "CREATE TABLE my_copy(".
	"code char(2) NOT NULL,".
	"religion char(20),".
	"percent float(10,2) NULL);";

$sth = $dbh->prepare ($q);
$sth->execute() or die("could not execute query");
print "created table\n";

$q = "SELECT * FROM Religions";
$sth = $dbh->prepare ($q);
$sth->execute() or die("could not execute query");
print "select success\n";

while(($code,$religion,$percent) = $sth->fetchrow_array()) {
   $q = "INSERT INTO my_copy (code, religion, percent) VALUES ('".
    $code."', '".
    $religion."', '".
    $percent."');";

   $sth1 = $dbh->prepare ($q);
   $sth1->execute() or die("could not execute query");

}

print "table copy success\n";

$dbh->disconnect();