Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id DFC55633FE6 for ; Mon, 31 Aug 2009 10:50:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 29069-04 for ; Mon, 31 Aug 2009 13:50:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outbound.vnet-inc.com (outbound.vnet-inc.com [216.146.104.103]) by mail.postgresql.org (Postfix) with ESMTP id 996D96321A7 for ; Mon, 31 Aug 2009 10:50:20 -0300 (ADT) Received: from [192.168.157.32] (unknown [216.129.226.113]) by outbound.vnet-inc.com (Postfix) with ESMTP id C1E7B5954; Mon, 31 Aug 2009 07:50:16 -0600 (MDT) Message-ID: <4A9BD287.9080000@ttc-cmc.net> Date: Mon, 31 Aug 2009 07:39:19 -0600 From: dmp User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.13) Gecko/20060417 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Damiano Bolzoni CC: pgsql-jdbc@postgresql.org Subject: Re: Search content within a bytea field References: <6fb522db0908301333o3bfeb6e9l5c2ad9dd04308e18@mail.gmail.com> In-Reply-To: <6fb522db0908301333o3bfeb6e9l5c2ad9dd04308e18@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=AWL=0.000, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/95 X-Sequence-Number: 19944 > > >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