pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: dmp <danap@ttc-cmc.net>
To: Damiano Bolzoni <damiano.bolzoni@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Search content within a bytea field
Date: Mon, 31 Aug 2009 07:39:19 -0600
Message-ID: <4A9BD287.9080000@ttc-cmc.net> (raw)
In-Reply-To: <6fb522db0908301333o3bfeb6e9l5c2ad9dd04308e18@mail.gmail.com>
References: <6fb522db0908301333o3bfeb6e9l5c2ad9dd04308e18@mail.gmail.com>

>
>
>Hi all,
>I'm trying to search for some content within a bytea field. My
>PreparedStatement looks like this:
>
>SELECT id FROM mytable WHERE myfield LIKE ?
>
>Now, when I execute a
>
>ps.setString(1, "%" + value + "%")
>
>where 'value' is the value I want to search for, the PS becomes
>
>SELECT id FROM mytable WHERE myfield LIKE %VALUE%
>
>and the query clearly fails ("operator does not exist: bytea ~~
>character varying"). Now, my question is....why? What am I doing
>wrong?
>
>Thanks for you help!
>

The problem I believe is in your prepare statement ps.setString(). The 
statement
using a string, but your field is bytea, try a cast.

>SELECT myfield FROM mytable WHERE myfield LIKE '%hello%'
>
>and 'myfield' is a bytea field I get the result I expected? What's the
>"trick" the client uses behind the curtains?
>

Of course this work, example given below, because you are not using a 
prepare
statement. PostgreSQL expects the exact type to be used in prepare 
statements.
These fields can be searched, but if the content is binary I don't think 
this general
approach is going to work. The content below for the dump is octal I 
believe, but
does contain the text "find" in the id = 2. The select statement for 
search in
MyJSQLView also shown, that does successfully find the row.

danap.

--
-- MyJSQLView SQL Dump
-- Version: 3.03
-- WebSite: http://myjsqlview.org
--
-- Host: 127.0.0.1
-- Generated On: 2009.08.31 AD at 07:20:14 MDT
-- SQL version: PostgreSQL 8.3.3
-- Database: postgresql_datatypes
--

-- ------------------------------------------

--
-- Table structure for table "public"."bytea_types"
--

DROP TABLE IF EXISTS "public"."bytea_types";
CREATE TABLE "public"."bytea_types" (
    "data_type_id" serial NOT NULL,
    "bytea_type" bytea DEFAULT NULL,
    PRIMARY KEY ("data_type_id")
);

--
-- Dumping data for table "public"."bytea_types"
--

LOCK TABLE "public"."bytea_types";
INSERT INTO "public"."bytea_types" ("data_type_id", "bytea_type") 
VALUES('1', E'\\054\\054\\041\\041\\041\\043\\043');
INSERT INTO "public"."bytea_types" ("data_type_id", "bytea_type") 
VALUES('2', 
E'\\152\\165\\163\\164\\040\\163\\157\\155\\145\\040\\155\\157\\162\\145\\040\\146\\151\\156\\144\\040\\150\\145\\154\\154\\157');

TableTabPanel_PostgreSQL actionPerformed() Connection Created
SELECT "data_type_id" FROM "public"."bytea_types" WHERE 
"bytea_type"::TEXT LIKE '%find%' ORDER BY "data_type_id" ASC LIMIT 50 
OFFSET 0
TableTabPanel_PostgreSQL actionPerformed() Connection Closed




view thread (13+ messages)  latest in thread

Message-ID: <4A9BD287.9080000@ttc-cmc.net>
Permalink:  ../4A9BD287.9080000@ttc-cmc.net/
Also on:    postgresql.org/message-id/4A9BD287.9080000@ttc-cmc.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: danap@ttc-cmc.net, damiano.bolzoni@gmail.com
  Subject: Re: Search content within a bytea field
  In-Reply-To: <4A9BD287.9080000@ttc-cmc.net>
  References: <6fb522db0908301333o3bfeb6e9l5c2ad9dd04308e18@mail.gmail.com> <4A9BD287.9080000@ttc-cmc.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox