perl变量写入数据库
我用正则表达式把一行文本解析了,用()分组放到变量$1,$2,$3中,然后怎么把这三个变量写到已有的数据库中?代码大致是://取各个信息if($entry=~m/^(\d...
我用正则表达式把一行文本解析了,用()分组放到变量$1,$2,$3中,然后怎么把这三个变量写到已有的数据库中?代码大致是:
//取各个信息
if ( $entry =~ m/^(\d{4}-\d{1,2}-\d{2})\s #Date
(\d{2}:\d{2}:\d{2})\s #Time
(\w*)$/x) #Module
{
print "The Date was '$1'.\n";
print "The Time was '$2'.\n";
print "The Module was '$3'.\n";
#上面三个的打印是ok的,说明已经取出来了
#入库,我已经连好了库了,想写入log表中()定义也ok
my $sth = $dbh->prepare( q{
INSERT INTO log (event_date,event_time,event_module) VALUES ($1,$2,$3)
}) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";
调试说$1,$2,$3的引用不对,也就是这句格式不对
INSERT INTO log (event_date,event_time,event_module) VALUES($1,$2,$3)
我刚开始学perl,只是想简单的用一下。是不是在SQL语句里面引用变量有别的书写规则?
变量写入数据库应该怎么写?
上面那个问题已经搞定。
再问一个白痴的问题,怎么统计总的记录数?
我这么写的,可是总是输出-1
my $sth = $dbh->prepare( q{
select count(*) as Total from log
}) or die "Can't prepare statement: $DBI::errstr";
my $num = $sth->execute()
or die "Can't execute statement: $DBI::errstr";
print $num;
print $num 展开
//取各个信息
if ( $entry =~ m/^(\d{4}-\d{1,2}-\d{2})\s #Date
(\d{2}:\d{2}:\d{2})\s #Time
(\w*)$/x) #Module
{
print "The Date was '$1'.\n";
print "The Time was '$2'.\n";
print "The Module was '$3'.\n";
#上面三个的打印是ok的,说明已经取出来了
#入库,我已经连好了库了,想写入log表中()定义也ok
my $sth = $dbh->prepare( q{
INSERT INTO log (event_date,event_time,event_module) VALUES ($1,$2,$3)
}) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";
调试说$1,$2,$3的引用不对,也就是这句格式不对
INSERT INTO log (event_date,event_time,event_module) VALUES($1,$2,$3)
我刚开始学perl,只是想简单的用一下。是不是在SQL语句里面引用变量有别的书写规则?
变量写入数据库应该怎么写?
上面那个问题已经搞定。
再问一个白痴的问题,怎么统计总的记录数?
我这么写的,可是总是输出-1
my $sth = $dbh->prepare( q{
select count(*) as Total from log
}) or die "Can't prepare statement: $DBI::errstr";
my $num = $sth->execute()
or die "Can't execute statement: $DBI::errstr";
print $num;
print $num 展开
1个回答
展开全部
my $sth = $dbh->prepare("INSERT INTO log (event_date,event_time,event_module) VALUES ('$1','$2','$3')") or die "Can't prepare statement: $DBI::errstr";
试试看这个。
my $num = $sth->execute() ;
这个地方$num值是执行execute的返回值哦,试下:
$sth->execute;
my @row=$sth->fetchrow_array;
$num=$row[0];
print $num;
试试看这个。
my $num = $sth->execute() ;
这个地方$num值是执行execute的返回值哦,试下:
$sth->execute;
my @row=$sth->fetchrow_array;
$num=$row[0];
print $num;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询