X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A94245289A for ; Tue, 14 Jun 2005 04:20:52 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79537-09 for ; Tue, 14 Jun 2005 07:20:43 +0000 (GMT) Received: from tnet.dp.ua (mail.tnet.dp.ua [217.196.171.1]) by svr1.postgresql.org (Postfix) with ESMTP id AD90452889 for ; Tue, 14 Jun 2005 04:20:40 -0300 (ADT) Received: from [217.196.171.125] (HELO [10.20.20.3]) by tnet.dp.ua (CommuniGate Pro SMTP 4.2.7) with ESMTP id 1324042 for pgsql-jdbc@postgresql.org; Tue, 14 Jun 2005 10:20:39 +0300 Message-ID: <42AEAF72.4050500@tnet.dp.ua> Date: Tue, 14 Jun 2005 10:20:34 +0000 From: Sergey Pariev User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: ru-ru, ru MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: in(...) clause and PreparedStatement References: <42AD8567.10004@tnet.dp.ua> <18349492-DC94-4DA4-B924-0FFC02F6A95B@fastcrypt.com> <1118669412.2720.11.camel@coppola.muc.ecircle.de> In-Reply-To: <1118669412.2720.11.camel@coppola.muc.ecircle.de> Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200506/65 X-Sequence-Number: 13027 Thanks to all for the instant and detailed replies. I have to say I posted simplified example of the query I need to run, in reality I don't know how many items will be in the IN clause. I have to do 3 queries (update, then insert ... select from this table and delete ) on particular table given the set of keys which I retrieve in the other query, which it complex so I can't dublicate it 3 times - so I can't just write SELECT * FROM mytable WHERE t_id IN (select t_id from other_table where ...). Currently I retrieve keys at first and concatenate them into string like 1,2,3,... , then I issue my queries like st.executeUpdate("update mytable set a_field = 1 where t_id IN ("+keys+")" ; and so on. It is working as for now, but is really ugly, so I desided to improve things and was just wandering if there a way to use variable number of parameters. Considering the thing you guys wrote I'm propably will rewrite my code to use fixed number of parameters - it wouldn' t be too hard since I'm processing it in chunks anyway. It will be only in the next version of my app though - don't want to fix the thing which are working :). Thanks again, Sergey. Csaba Nagy пишет: >Sergey, > >Additionally to what Dave wrote you: if you want to use the prepared >statement with variable number of parameters, you can sometimes use a >query with a fixed number of parameters, and if you have more >parameters, execute it chunk-wise, if you have less parameters then set >the additional ones to null. This variant complicates your code >considerably though, but the query is prepared and reusable. I would >think it only matters if you reuse the prepared statement for a large >number of executions. > >This only works if you don't have to have all the parameters processed >in one statement, i.e. you can chunk your query and the final cumulated >results will be the same. > >In any other case you will not be able to use prepared statements, i.e. >you'll need to build your query each time. It is still advisable to do >it via JDBC prepared statements, and not build it directly by hand, >because that will take care for all the escaping necessary for your >parameter values. This means you should build a query with as many ? >signs as many parameters you have, and then set them in a loop or so. > >HTH, >Csaba. > > >On Mon, 2005-06-13 at 15:07, Dave Cramer wrote: > > >>Yes, because it thinks "1,2,3" is a string >> >>you would have to do >> >>IN(?,?,?) >> >>then >> >>setObject(1, 1); >>setObject(2, 2); >>setObject(3, 3); >> >>Dave >> >>On 13-Jun-05, at 9:08 AM, Sergey Pariev wrote: >> >> >> >>>Hi all. >>> It may be stupid question, but is there way to use >>>PreparedStatement with queries like >>>SELECT * FROM mytable WHERE t_id IN (1,2,3) ? >>> I've googled but haven't found nothing explicitly said on this >>>topic. >>> I've tried the following >>> PreparedStatement st = conn.prepareStatement("SELECT * FROM >>>mytable WHERE t_id IN ( ? )"); >>> st.setObject(1,"1,2,3"); >>> >>> and get error complaining on type mismatch. >>> >>> Thanks in advance, Sergey. >>> >>>---------------------------(end of >>>broadcast)--------------------------- >>>TIP 2: you can get off all lists at once with the unregister command >>> (send "unregister YourEmailAddressHere" to >>>majordomo@postgresql.org) >>> >>> >>> >>> >>---------------------------(end of broadcast)--------------------------- >>TIP 6: Have you searched our list archives? >> >> http://archives.postgresql.org >> >> > > >