我需要使用 perl 将日期时间值插入到 Oracle 数据库表中。以下代码插入所需的值:
use strict;
use warnings;
use DBI ;
use DBD::Oracle ;
my $SRV1='server1';
my $DB1='database1';
my $Date_Str_To_Insert="TO_DATE('2003/05/03 21:02:44','yyyy/mm/dd hh24:mi:ss')";
my $db1 = DBI->connect("dbi:Oracle:$SRV1/$DB1", "user", "pword");
my $SQL1="insert into table1 (valdat) values ($Date_Str_To_Insert)";
my $SQL1_sth = $db1->prepare($SQL1);
$SQL1_sth->execute();
$SQL1_sth->finish();
$db1->disconnect if defined($db1) ;
Run Code Online (Sandbox Code Playgroud)
但是,如果我更换
my $SQL1="insert into table1 (valdat) values ($Date_Str_To_Insert)";
Run Code Online (Sandbox Code Playgroud)
经过
my $SQL1="insert into table1 (valdat) values (?)";
Run Code Online (Sandbox Code Playgroud)
和
$SQL1_sth->execute();
Run Code Online (Sandbox Code Playgroud)
经过
$SQL1_sth->execute($Date_Str_To_Insert);
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误:
DBD ERROR: error possibly near <*> indicator at char 42 in 'insert into t_datatable (valdat) values (:<*>p1)') [for Statement "insert into t_datatable (valdat) values (?)" with ParamValues: :p1='TO_DATE('2021-08-04 11:03:05','yyyy-mm-dd hh24:mi:ss')'] at perltestprog.pl line 6977.
Run Code Online (Sandbox Code Playgroud)
如何定义变量 $Date_Str_To_Insert 以便它使用问号表示法工作?
试试这个:
my $SQL1 = "insert into table1 (valdat) values (TO_DATE(?,'yyyy/mm/dd hh24:mi:ss'))";
my $SQL1_sth = $db1->prepare($SQL1);
$SQL1_sth->execute('2003/05/03 21:02:44');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |