Return-Path: X-Original-To: apmail-db-derby-user-archive@www.apache.org Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E16A410FEB for ; Wed, 23 Oct 2013 20:03:03 +0000 (UTC) Received: (qmail 17518 invoked by uid 500); 23 Oct 2013 20:01:31 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 17197 invoked by uid 500); 23 Oct 2013 20:00:25 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 16401 invoked by uid 99); 23 Oct 2013 19:59:38 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Oct 2013 19:59:38 +0000 X-ASF-Spam-Status: No, hits=-2.3 required=5.0 tests=RCVD_IN_DNSWL_MED,SPF_PASS,UNPARSEABLE_RELAY X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of rick.hillegas@oracle.com designates 156.151.31.81 as permitted sender) Received: from [156.151.31.81] (HELO userp1040.oracle.com) (156.151.31.81) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Oct 2013 19:59:30 +0000 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by userp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r9NJx8UZ005596 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 23 Oct 2013 19:59:09 GMT Received: from aserz7021.oracle.com (aserz7021.oracle.com [141.146.126.230]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r9NJx7QF004755 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 23 Oct 2013 19:59:08 GMT Received: from abhmt116.oracle.com (abhmt116.oracle.com [141.146.116.68]) by aserz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r9NJx7PS024983 for ; Wed, 23 Oct 2013 19:59:07 GMT Received: from dhcp-whq-twvpn-3-vpnpool-10-159-233-55.vpn.oracle.com (/10.159.233.55) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 23 Oct 2013 12:59:07 -0700 Message-ID: <52682A91.3040302@oracle.com> Date: Wed, 23 Oct 2013 12:59:13 -0700 From: Rick Hillegas User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.7; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 To: derby-user@db.apache.org Subject: Re: deleting a record - still a problem References: <1382499862370-134975.post@n7.nabble.com> In-Reply-To: <1382499862370-134975.post@n7.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Source-IP: ucsinet21.oracle.com [156.151.31.93] X-Virus-Checked: Checked by ClamAV on apache.org On 10/22/13 8:44 PM, Bob M wrote: > Hi there > > The error is that my SELECT can not have "For UPDATE" ???? > > My current code is > *************************** > // retrieve and output date and time of oldest record from the table > rs = s.executeQuery("SELECT * FROM tablename ORDER BY Date ASC, Time ASC > FETCH FIRST ROW ONLY"); > rs.next(); > String Date2 = rs.getString("Date"); > String Time2 = rs.getString("Time"); > > myConsole.getOut().println("Date/Time: " + Date2 + ", " + Time2); > > // and now delete this record............. > s.setCursorName("MYCURSOR"); > rs = s.executeQuery("SELECT * FROM tablename ORDER BY Date ASC, Time ASC > FETCH FIRST ROW ONLY FOR UPDATE"); > rs.next(); > > psDelete = conn.prepareStatement("DELETE FROM tablename WHERE CURRENT OF > MYCURSOR"); > statements.add(psDelete); > > myConsole.getOut().println("Deleted oldest record"); > > > > -- > View this message in context: http://apache-database.10148.n7.nabble.com/deleting-a-record-still-a-problem-tp134975.html > Sent from the Apache Derby Users mailing list archive at Nabble.com. > Hi Bob, Another way to solve this problem would be to add a unique key column to your table and use that to specify which row should be deleted. The following script shows how this would work. Hope this helps, -Rick ----------------- connect 'jdbc:derby:memory:db;create=true'; -- create a little procedure to pause between inserts create procedure sleep( millis bigint ) language java parameter style java no sql external name 'java.lang.Thread.sleep'; create table t( id int generated always as identity, dateCol date, timeCol time ); insert into t( dateCol, timeCol ) values ( current date, current time ); call sleep( 1000 ); insert into t( dateCol, timeCol ) values ( current date, current time ); call sleep( 1000 ); insert into t( dateCol, timeCol ) values ( current date, current time ); select * from t order by dateCol, timeCol; select id from t order by dateCol, timeCol fetch first 1 row only; delete from t where id = ( select id from t order by dateCol, timeCol fetch first 1 row only ); select * from t order by dateCol, timeCol;