perl插入数据库的占位符
时间:2008-07-22 来源:hoo7
my $testdb = DBI->connect( "dbi:Sybase:server=DBIP;database=DB",
"user","password",{AutoCommit=>0} )
or die "$DBI::errstr\n"; my $sth = $testdb->prepare (qq{insert into orders (order_id) values (?)}); foreach my $num (1..1000) {
$sth->execute("$num");
$num++;
} $sth->commit();
$testdb->disconnect(); 请注意这个INSERT 查询中的‘?’就是一个占位符。调用execute( ) 时,将查询发送给服务器,传递这个值来代替占位符。一般来说,如果发现在循环内部调用了do( ),应该在循环前调用prepare( ),并在这个循环内部调用execute( ) 更好一些。
有关占位符的一些注意事项:
在查询字符串内,不要在引号中封装占位符字符。如果这样做,不能识别为占位符。
不要使用quote( ) 方法来指定占位符的值,否则将在插入的值中得到额外的引号。
在查询字符串中可以有一个以上的占位符,但是要确保占位符的标记符与传递给execute( ) 的值一样多。
每个占位符都必须指定一个单独的值,而不是一列值。为了将NULL 指定为占位符,应该使用undef。
相关阅读 更多 +