Perl无法连接数据库

时间:2018-08-06 05:27:07

标签: database perl

我是Perl编程的新手。我尝试了基本的perl数据库连接,并得到以下错误。请任何人帮助解决此问题。

"use" not allowed in expression at ./database.pl line 4, at end of line
syntax error at ./database.pl line 4, near "use DBI
use strict"
Execution of ./database.pl aborted due to compilation errors.

database.pl文件

#!/usr/bin/perl

use DBI
use strict;

my $driver = "mysql";
my $database = "pj_order";
my $dsn = "DBI:$driver:database=$database;host=localhost";
my $userid = "root";
my $password = "root";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;

最后,我已经运行了database.pl文件,并出现以下错误。

"use" not allowed in expression at ./database.pl line 4, at end of line
syntax error at ./database.pl line 4, near "use DBI
use strict"
Execution of ./database.pl aborted due to compilation errors.

1 个答案:

答案 0 :(得分:3)

use DBI

应该是

use DBI;

最好放在后面

use strict;

您还应该拥有

use warnings qw( all );