in reply to Class::DBI / Oracle date field Q

Class::DBI is (wisely) quoting all of your input before it puts it in an SQL query. What the database sees is something like this:
update tablename set foo = 'bar', date = 'to_date( \'03/03/2004\', \'MM/DD/YYYY\' )' where id = '1'
It tries to set the date column to the string "to_date( '03/03/2004', 'MM/DD/YYYY' )" and fails.

You'll have to do any date calculations & conversions yourself and pass in the result using whatever format Oracle recognizes (unix epoch? YYYYMMDDHHMMSS format?). Or maybe even use attribute inflation/deflation to make your life a little easier. Just pass in (for instance) a Time::Piece object instead of a date string. There's even an example of this using Time::Piece in Class::DBI's POD.

Bottom line: For safety reasons, anything you set your Class::DBI objects' fields to will never be interperted as SQL. Otherwise, its queries would fail every time you did something involving quotation marks.

blokhead

Replies are listed 'Best First'.
Re: Re: Class::DBI / Oracle date field Q
by freddo411 (Chaplain) on May 07, 2004 at 22:13 UTC
    Thanks for the helful advice. At the moment, under time pressure, I don't grok the details of inflation, but I get the general idea.

    It seems that with Class::DBI only numbers and strings can be passed into the DB.

    Here's a follow up so that everyone knows what I did to fix the problem:

    Oracle will by default turn a string into a date datatype based upon only its very strict idea of what a date string should be patterned like. I am using Oracle 8i, which will allow me to set the default date pattern for my session. I will do this so that I have a predictable pattern to make my strings. I do this in my Class::DBI setup like so:

    package CDNR::DBI; use base 'Class::DBI::Oracle'; use Config::General; my %hostCF = ParseConfig("../mymodules/config.$hostname"); CDNR::DBI->set_db('Main', $hostCF{'dbconnect'}, $hostCF{'dbname'}, $hostCF{'dbpw'}); my $dbh = CDNR::DBI->db_Main; my $qs = "Alter session set nls_date_format = 'Mon-DD-YYYY'"; $dbh->do($qs) or die "DB err alter date";

    At this point I can pass a string to the Class::DBI for inserting into a "date" database field as longs as the string looks like Mon-DD-YYYY.

    cheers

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

Re: Re: Class::DBI / Oracle date field Q
by AZPhantom (Initiate) on May 08, 2004 at 00:20 UTC
    As I understand the DBI, the quote function is automaticly called for prepare() but not for do(). So in this case passing a $q = $dbh->quote($q); before the do() instead of trying to \ the quotes might help.