我创建了一个 SQLite 数据库和一个名为' COMPANY'的表。
我的目的是创建一个模块( Connect.pm ),它保护数据库处理程序($ dbh),这是与数据库的连接,并与对象保持一致创建后,我可以调用Connect.pm中的插入方法。
当我尝试将数据插入数据库时,它给出了以下错误:
DBD :: SQLite :: db prepare failed:没有这样的表:temp.pl上的COMPANY 第6行。
DBD :: SQLite :: db准备失败:没有这样的表:COMPANY at temp.pl第6行。
Connect.pm
package Connect;
use strict;
use DBI;
use Data::Dumper;
sub new {
my $class = shift;
my $driver = "SQLite";
my $database = "WEBSITE.db";
my $dsn = "DBI:$driver:dbname:$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1}) or die $DBI::errstr;
my $self = { dbh => $dbh};
print STDERR "Opened database successfully\n";
bless $self, $class;
return $self;
}
sub insert {
my $self = shift;
my ($id, $firstName, $lastName, $email, $comment) = @_;
my $sth = $self->{ dbh }->prepare("INSERT INTO COMPANY VALUES (?, ?, ?, ?, ?)");
my $rv = $sth->execute($id, $firstName, $lastName, $email, $comment) or die $DBI::errstr;
my $value = "Inserted Successfully";
if($rv < 0){
$value = $DBI::errstr;
}
return $value;
}
1;
temp.pl
use DBI;
use strict;
use Connect;
my $obj = Connect->new();
my $status = $obj->insert("002", "Test", "User", "a\@b.com", "Comment");
$obj->disconenct();
我可以使用这个程序来创建数据库并查询它,它可以工作。
use DBI;
use strict;
my $driver = "SQLite";
my $database = "WEBSITE.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect( $dsn, $userid, $password, { RaiseError => 1 } ) or die $DBI::errstr;
my $sth = $dbh->prepare('INSERT INTO COMPANY VALUES (?, ?, ?, ?, ?)');
my $rv = $sth->execute( "003", "Test", "User", "a\@b.com", "Test Comment" ) or die $DBI::errstr;
答案 0 :(得分:1)
模块中的DSN错误:“DBI:$ driver:dbname:$ database”应该是“DBI:$ driver:dbname = $ database”(dbname之后应该有一个等号,而不是冒号)。发表于ThisSuitIsBlackNot 1月10日15:57是正确答案...