简体   繁体   English

PERL DBI:将.pl插入Oracle DB的to_date函数转换为测试变量

[英]PERL DBI: Inserting From .pl into an Oracle DB a to_date function into a test variable

First a big thank you to all PERL specialist in this wonderful website; 首先非常感谢这个精彩网站上的所有PERL专家; i'am learning so much. 我学到了很多东西。

I have a small "translation" issue; 我有一个小的“翻译”问题;

my .pl script perform these task sequentially: 我的.pl脚本按顺序执行这些任务:

  1. Read a .txt file (in real life a log) 读一个.txt文件(在现实生活中一个日志)
  2. a regex cleans and store in memory variables some info 正则表达式清除并在内存变量中存储一些信息
  3. while reading line by line it inserts into a Oracle table 在逐行阅读时,它会插入到Oracle表中
  4. Exit and disconnect when finish gracefully. 正常完成后退出并断开连接。

The Table has 8 columns and one of this store date and time of an event and the format that i declared into sql is: 表有8列,其中一个是事件的存储日期和时间,我在sql中声明的格式是:

DATETIME_EVENT VARCHAR2(255 BYTE) DEFAULT NULL NOT NULL 

you can clearly see where i am going in a bit.... 你可以清楚地看到我的位置......

so point 3 above execute this block: 所以上面的第3点执行这个块:

my $sth = $dbh->prepare('INSERT INTO myTable(TIME_ISSUE,col2,col3,col4,col5,col6,col7,col8) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');

EXAMPLE INSERTED IS: 2014-09-19 00:03:58,562 示例插入IS:2014-09-19 00:03:58,562

Everything is smooth and fine , however I will perform set of queries later on; 一切都很顺利,但我稍后会执行一系列查询; but I will have to translate with to_date function in SQL to interpret TIME_ISSUE column as datetime TYPE. 但我必须在SQL中使用to_date函数进行翻译,以将TIME_ISSUE列解释为datetime TYPE。

AS: 如:

select to_date(SUBSTR(TIME_ISSUE,0,19) ,'YYYY-MM-DD HH24:MI:SS' ) from myTable;

OUTPUT: 19/09/2014 23:59:56 输出日期:19/09/2014 23:59:56

If I create the table.column as datetime type how can i write the perl insert block above in order to insert a txt into datetime type? 如果我创建table.column作为datetime类型,我如何编写上面的perl插入块以便将txt插入到datetime类型中?

I tried but is not working at all: 我试过但根本不工作:

my $sth = $dbh->prepare('INSERT INTO REQUEST (TO_DATE(SUBSTR(DATETIME_EVENT ,0,19), 'YYYY-MM-DD HH24:MI:SS'),LOG_LEVEL,LOGONID, CLIENTID, USERID,IPADDRESS,DURATION,CLASSMETHOD) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')

ps PS

you can assume that if i run the perl above i have previously created that column as this: 你可以假设,如果我运行上面的perl,我之前创建了这个列:

TIME_ISSUE   DATE DEFAULT NOT NULL                       

You can also change the date format for the session. 您还可以更改会话的日期格式。 This is useful, for example, in Perl DBI, where the to_date() function is not available: 例如,在Perl DBI中,这很有用,其中to_date()函数不可用:

ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'

You can permanently set the default nls_date_format as well: 您也可以永久设置默认的nls_date_format:

ALTER SYSTEM SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'

In Perl DBI you can run these commands with the do() method: 在Perl DBI中,您可以使用do()方法运行这些命令:

$db->do("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS');

http://www.dba-oracle.com/t_dbi_interface1.htm https://community.oracle.com/thread/682596?start=15&tstart=0 http://www.dba-oracle.com/t_dbi_interface1.htm https://community.oracle.com/thread/682596?start=15&tstart=0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM