Hello monks,
I have written a script that I intended to use to escape binary data that I plan to insert into a bytea field into a postgresql database. the specs for the escaping are here: http://www.postgresql.org/docs/8.1/static/datatype-binary.html see table 8-7
my script works, but apparently not correctly because the DB won't take the escaped data. I am returned: invalid input syntax for type bytea
I would like some feed back on what I might be missing.
#!/usr/bin/perl
open(BINFILE, "your favorite binary file here");
binmode(BINFILE);
$escaped = "";
while(read(BINFILE, $char, 1)){
$char = escape(ord($char));
$escaped .= $char
}
print $escaped, "\n";
sub escape {
$ordinal = shift();
%escapeList = (0 => "\\\\000",
1 => "\\\\001",
2 => "\\\\002",
3 => "\\\\003",
4 => "\\\\004",
5 => "\\\\005",
6 => "\\\\006",
7 => "\\\\007",
8 => "\\\\008",
9 => "\\\\009",
10 => "\\\\010",
11 => "\\\\011",
12 => "\\\\012",
13 => "\\\\013",
14 => "\\\\014",
15 => "\\\\015",
16 => "\\\\016",
17 => "\\\\017",
18 => "\\\\018",
19 => "\\\\019",
20 => "\\\\020",
21 => "\\\\021",
22 => "\\\\022",
23 => "\\\\023",
24 => "\\\\024",
25 => "\\\\025",
26 => "\\\\026",
27 => "\\\\027",
28 => "\\\\028",
29 => "\\\\029",
30 => "\\\\030",
31 => "\\\\031",
32 => "\\\\032",
39 => "\\\\047",
92 => "\\\\134",
);
if(defined($escapeList{$ordinal})){
return $escapeList{$ordinal};
}
else{
return chr($ordinal);
}
}
Thanks.
p.s. i used this ascii reference as a guide: http://www.lookuptables.com/
FIXED: Never mind I am stupid. I was reading the wrong column for the conversion. Of course it is rejected because the codes are in HEX, NOT OCT. Sorry for wasting the post space.
Update: Now that I just verified that it works fine with the correct escape codes, would any one mind suggesting a cleaner, more elegant method than what I have?
Thanks, again.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.