Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: DBI converting hex values

by Sprad (Hermit)
on Nov 02, 2006 at 19:05 UTC ( #581949=note: print w/replies, xml ) Need Help??

in reply to Re: DBI converting hex values
in thread DBI converting hex values

In this particular case, the data type is a custom type derived from Binary. But my code doesn't know that, it gets queries passed in from outside. My code runs them (managing all the DB connection details) and passes back the results.

As for how I'm seeing the value, I'm using Data::Dumper and print statements. I could convert it back to a string representation of the hex value, but only if I can know which fields got changed in this way. Since the SQL code could be pretty much anything, I'm not sure if this is possible.

I'd like to push this burden off to the calling scripts, since they know what their SQL is. But my intermediate layer previously used another DB module (Sybase::CTLib), which returned the hex values in the way I want. I've got to make my layer behave the same way, or I'll break all the other scripts.

A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re^3: DBI converting hex values
by VSarkiss (Monsignor) on Nov 02, 2006 at 20:29 UTC

    OK, now we're getting somewhere. :-)

    So the underlying database is Sybase, and you used to use Sybase::CTLib to handle some VARBINARY-ish type. If so, and if you have the option, the easiest choice may be to switch to DBD::Sybase instead of DBD::ODBC. Then a lot of the behavior should be as you expect. (The last time I used Sybase was about five years ago, so things may have changed.)

    I think I understand your problem: you know how to format the columns as string or hex, you just don't know which ones should be formatted in each way. If that's the case, then you may want to look at the TYPE attribute of the statement handle (documented in DBI, usually used along with the type_info method), which will allow you to find the underlying type of each result column. It sounds like that may give you enough information to decide how to format each column. Something like this (cheesy, untested) example:

    my $type_i_want = $dbh->type_info( [ SQL_whatever ] ); # # ...later... # $sth->execute(@parameters); my @types = $sth->{TYPE}; my $row = $sth->fetch(); for (my $i = 0; $i < $sth->{NUM_OF_FIELDS}; $i++) { if ($types[$i] eq $type_i_want) { printf "0x%x", $row[$i]; } else { printf "%s", $row[$i]; } }

    Hope that gets you closer.

Re^3: DBI converting hex values
by Fletch (Chancellor) on Nov 02, 2006 at 20:24 UTC

    There may be a way to get the underlying DBD layer to treat it as binary data using bind_col or bind_param (I can't remember which is for which direction; I think you want the former).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://581949]
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-12-12 00:53 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (320 votes). Check out past polls.