Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 8CF989FB51A; Mon, 12 Feb 2007 15:55:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 79351-04; Mon, 12 Feb 2007 15:55:09 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 7F7F39FB60D; Mon, 12 Feb 2007 15:55:05 -0400 (AST) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 0C3B176C94; Mon, 12 Feb 2007 19:50:57 +0000 (GMT) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 8AA58274160; Mon, 12 Feb 2007 19:50:49 +0000 (GMT) Message-ID: <45D0C519.2080107@archonet.com> Date: Mon, 12 Feb 2007 19:50:49 +0000 From: Richard Huxton User-Agent: IceDove 1.5.0.9 (X11/20061220) MIME-Version: 1.0 To: Ezequias Rodrigues da Rocha Cc: Scott Marlowe , pgsql-sql@postgresql.org, pgsql-admin@postgresql.org Subject: Re: [SQL] Deadlock on transaction References: <55c095e90702120943v2909044eo91445b0eda3958c7@mail.gmail.com> <1171302488.3565.15.camel@state.g2switchworks.com> <55c095e90702121008u6d3d27c0m16e52d63f97c3e7d@mail.gmail.com> <45D0B565.9060402@archonet.com> <55c095e90702121047x1c66b098xbcf0d308195ff584@mail.gmail.com> <45D0B8A4.7000602@archonet.com> <55c095e90702121120s5b98a2d8u4a05a31f9625ffa@mail.gmail.com> In-Reply-To: <55c095e90702121120s5b98a2d8u4a05a31f9625ffa@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=0.227 tagged_above=0 required=5 tests=AWL, BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200702/168 X-Sequence-Number: 24633 Ezequias Rodrigues da Rocha wrote: > This is delphi. I don't intent you understand but the sql actions are quite > simple (I am reading a list of numbers). Well, let's see - the last Pascal I did was in 1986 I think... > If not dm.database1.InTransaction then > dm.database1.StartTransaction; > > For i:= 0 to memo1.Lines.Count - 1 do Loop through your import. > Begin > // Catching ID > dm.qQ1.SQL.text:= Concat('select id from base.cartao where > numero = ', memo1.lines[i]); > dm.qQ1.open; > cartaoID:= dm.qQ1.fieldByName('id').asString; > // Cathing the ticket ID OK, you're fetching ticket IDs one at a time. > dm.qQ1.SQL.clear; > dm.qQ1.SQL.text:= Concat('select nextval(',QuotedStr(' > base.ingresso_id') , ')'); > dm.qQ1.open; > IngressoID:= dm.qQ1.fieldByName('nextval').asString; And now you've generated a new ID from a sequence. > // $$$$$$$$$$ Recording Tickets $$$$$$$$$$ > dm.Qq1.sql.text:= Concat('Insert into base.ingresso values (' > , QuotedStr(IngressoID), ',' , EstadoID, ',' , 'now()', ',' , valor, ',' , > valor_promotor, ',' , AssentoID, ',', CaixaID, ',' , CartaoID, ',' , > PromocaoID, ',' , SessaoID, ',' , VendedorID, ')'); > // Showmessage(dm.Qq1.sql.text); > dm.Qq1.execSQL; Insert one row into "ingresso". > // ########### Recording Tickets ########### > > // Pegando o Id do Bilhete no PostgreSQL > dm.qQ1.SQL.clear; > dm.qQ1.SQL.text:= Concat('select nextval(',QuotedStr(' > base.bilhete_id') , ')'); > dm.qQ1.open; > BilheteID:= dm.qQ1.fieldByName('nextval').asString; > > dm.qQ1.SQL.clear; > dm.qQ1.SQL.add(Concat('Insert into base.bilhete (id, estado, > uso_sequencia, promocao_documento, assento_id, cartao_id, ingresso_id, > promocao_id, sessao_id, vendedor_venda_id )')); > dm.qQ1.SQL.add(Concat('values(', BilheteID, ',' , > QuotedStr(EstadoID), ',' , '0,0', ',' , QuotedStr(AssentoID), ',' , > QuotedStr(CartaoID), ',' , QuotedStr(IngressoID) , ',' , > QuotedStr(PromocaoID), ',' , QuotedStr(SessaoID) , ',' , > QuotedStr(VendedorID), ')' ) ); > > dm.qQ1.execSQL; Insert a row into "bilhete". > > end; I don't see anything like "dm.database1.CommitTransaction" - are you sure you are committing after the inserts? One other point - if Delphi doesn't offer something like it, you might find it useful to write a function that makes it easier to build queries: my_db_func('INSERT INTO foo (id,a,b,c) VALUES (?,?,?,?)', 'NUM,TEXT,DATE,DATE', variable1,variable2,variable3...); I'd be surprised if there wasn't something like that already though. -- Richard Huxton Archonet Ltd