in reply to Re^2: Debian DBD::ODBC select row by timestamp
in thread Debian DBD::ODBC select row by timestamp

my $sth = $dbh-> prepare (SELECT val from table where DD_MM_YYYY = ?); $sth->execute('2012-07-22 00:00:00');
? is the placeholder. It serves great use and it protects you against SQL injection attacks, by automatically quoting (and thereby neutralising) any "dangerous" characters.

Of course you should add the usual or die ... checks to make sure no error happened. Also you have to verify the format of the DD_MM_YYY field to make sure your query sends in the timestamp in the right format.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^4: Debian DBD::ODBC select row by timestamp
by alexx_sh (Novice) on Jul 24, 2012 at 05:38 UTC
    when i try your code i get err

    Search pattern not terminated at ./script.pl line 34.

    if i try code like this $dbh-> prepare ('SELECT val from table where DD_MM_YYYY = ?');

    Error at Line : syntax error near ?

      Can we see the script and the actual error messsage for the second case (including line number). The first case just needed quotes adding.

        Yes.
        use strict; use warnings; use DBI; use Data::Dumper; use JSON::XS; my $dbh = DBI->connect (q{DBI:ODBC:test}) or die $DBI::errstr; my $sth = $dbh-> prepare("SELECT val from buf WHERE dd_mm_yyyy= ? ") o +r die $dbh->errstr(); $sth->execute('2012-07-22 00:00:00') or die $dbh->errstr(); my $table; my $tbl_ary_ref; $tbl_ary_ref = $sth->fetchall_arrayref; if (defined $tbl_ary_ref){ $table->{date} = $tbl_ary_ref; } $tbl_ary_ref = encode_json $table; print qq{JSON-------------------JSON\n}; print $tbl_ary_ref."\n"; $sth->finish; $dbh->disconnect;
        And output:
        Error at Line : syntax error near ? JSON-------------------JSON {"date":[]}