我想连接sql server并执行sql查询 我使用下面的Perl代码
在此代码中,我使用了ADO DRIVER和odledb驱动程序
代码是
use DBI;
my $host = 'sqlservername';
my $database = 'db';
my $user = 'usr';
my $auth = 'Password';
# DBD::ADO
$dsn = "Provider=sqloledb;Trusted Connection=yes;";
$dsn .= "Server=$host;Database=$database";
my $dbh = DBI->connect("dbi:ADO:$dsn",
$user,
$auth,
{ RaiseError => 1, AutoCommit => 1}
) || die "Database connection not made: $DBI::errstr";
my $sql = "select col1,col2,col3 from tablename"
my $sth = $dbh->prepare($sql);
# Execute the statement
$sth->execute();
my( $id, $name, $phone_number );
$sth->bind_columns( undef, \$id, \$name, \$phone_number );
# Retrieve values from the result set
while( $sth->fetch() ) {
print "$id, $name, $phone_number\n";
}
# Close the connection
$sth->finish();
$dbh->disconnect();
错误是
syntax error at testdbconn.pl line 16, near "my "
Execution of testdbconn.pl aborted due to compilation errors.
请我解决方案
答案 0 :(得分:0)
在$ sql的以下初始化中缺少分号。
my $sql = "select col1,col2,col3 from tablename"
my $sth = $dbh->prepare($sql);