ActiveState是微軟轉投資的研發script語言如Perl、Python、Tcl在Windows下運行的執行環境,PPM(Perl Package Manager)下載DBD:Oracle連Oracle沒問題,但找不到DBD:MySQL,最後下載到DBD:mysqlPP,寫連結程式還是不行,最後是下面DNS才過關:

dbi:mysqlPP:database=$sid;host=$host

sample source code如下:

use DBI;

my $host = "127.0.0.1";
my $port = "3306";
my $sid = "database";
my $user = "root";
my $passwd = "root";

my $dbh = DBI->connect("dbi:mysqlPP:database=$sid;host=$host", $user, $passwd) or die "error:$!";

# Drop table 'foo'. This may fail, if 'foo' doesn't exist.
# Thus we put an eval around it.
eval { $dbh->do("DROP TABLE foo") };
print "Dropping foo failed: $@\n" if $@;

# Create a new table 'foo'. This must not fail, thus we don't
# catch errors.
$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");

# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");

# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");

# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();

# Disconnect from the database.
$dbh->disconnect();

arrow
arrow
    全站熱搜

    Jemmy 發表在 痞客邦 留言(0) 人氣()