#!/usr/bin/perl -w use DBI qw(:sql_types); use Time::HiRes qw ( time ); my $dbh= DBI->connect("DBI:mysql:test", "root") or die; $dbh->do("DROP TABLE IF EXISTS t1") or die; $dbh->do("CREATE TABLE t1 (c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY, c2 CHAR(10), c3 FLOAT(16,6))") or die; my $sth= $dbh->prepare("INSERT INTO t1 (c2, c3) VALUES (?, ?)") or die; # # INSERT 6 test rows # $sth->execute('1', 1); $sth->execute('2.1', 2.1); $sth->execute('"3"', "3"); $sth->execute('"4.4"', "4.4"); $sth->execute('pi', 3.1415926535); my $pi= 3.1415926535; $sth->execute('$pi', $pi); # # get current time from Time::HiRes # my $time= time; printf "\$time= %.6f\n", $time; # # INSERT 5 rows using Time::HiRes timestamp # $sth->execute('$time', $time); $sth->bind_param(1, '$time'); $sth->bind_param(2, $time, SQL_FLOAT); $sth->execute; $sth->bind_param(2, $time); $sth->execute; $sth->bind_param(2, 0.0+$time, SQL_FLOAT); $sth->execute; $sth->bind_param(2, 0.0+$time); $sth->execute; # # explicitly convert timestamp to numerical Perl type # my $time2= $time+0.0; printf "\$time2= %.6f\n", $time2; # # INSERT 3 rows using numerical value # $sth->execute('$time2', $time2); $sth->bind_param(1, '$time2'); $sth->bind_param(2, $time2, SQL_FLOAT); $sth->execute; $sth->bind_param(2, $time2); $sth->execute; # # explicitly convert timestamp to string # my $time3 = sprintf "%.6f", $time; printf "\$time3= %s\n", $time3; # # INSERT 2 rows using string value # $sth->execute('$time3', $time3); $sth->bind_param(1, '$time3'); $sth->bind_param(2, $time3); $sth->execute; # # SELECT all rows # $sth= $dbh->prepare("SELECT * FROM t1"); $sth->execute; DBI::dump_results($sth);