<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Nov 14, 2014 at 3:23 PM, Michael Van Canneyt <span dir="ltr"><<a href="mailto:michael@freepascal.org" target="_blank">michael@freepascal.org</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">
On Fri, 14 Nov 2014, Reimar Grabowski wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
On Fri, 14 Nov 2014 14:50:20 +0100<br>
Reimar Grabowski <<a href="mailto:reimgrab@web.de" target="_blank">reimgrab@web.de</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
On Fri, 14 Nov 2014 14:15:08 +0100 (CET)<br>
Michael Van Canneyt <<a href="mailto:michael@freepascal.org" target="_blank">michael@freepascal.org</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Connections for databases that have lastinsertID you can call the method created for this:<br>
<br>
Function GetInsertID: int64;<br>
</blockquote></blockquote>
Unfortunately it does not work and returns only 0, while my custom query using LAST_INSERT_ID() works as expected.<br>
Debugging this is very hard as Lazarus crashes on me left and right when trying to step through the code and an exception is encountered.<br>
I have one connection, one transaction and some queries bound to them on a FPWebModule.<br>
<br>
ID1:=MySQL55Connection1.<u></u>GetInsertID;<br>
SQLQuery1.Open;<br>
ID2:=SQLQuery1.FieldByName('<u></u>LastInsertID').AsLargeInt;<br>
<br>
ID1 is 0 and ID2 the correct id value.<br>
<br>
Any ideas what I could be doing wrong?<br>
</blockquote>
<br></span>
This is what it does:<br>
<br>
function TConnectionName.GetInsertID: Int64;<br>
begin<br>
CheckConnected;<br>
Result:=mysql_insert_id(<u></u>GetHandle);<br>
end;<br>
<br>
a) Did you do an insert right before the call to GetInsertID ?<br>
b) Is the transaction active after the insert ? c) The connection transaction should be the same as the transaction of the insert.</blockquote></div><div><br></div><div>Work fine here (MySQL 5.5 32 bits / Lazarus 1.2.6 r46529 FPC 2.6.4 i386-win32-win32/win64 / Windows 7 64 bits):</div><div><br></div><div>unit1.lfm:</div><div><br></div><div><div>object Form1: TForm1</div><div> Left = 238</div><div> Height = 240</div><div> Top = 144</div><div> Width = 320</div><div> Caption = 'Form1'</div><div> ClientHeight = 240</div><div> ClientWidth = 320</div><div> LCLVersion = '1.2.6.0'</div><div> object Button1: TButton</div><div> Left = 10</div><div> Height = 25</div><div> Top = 8</div><div> Width = 75</div><div> Caption = 'Button1'</div><div> OnClick = Button1Click</div><div> TabOrder = 0</div><div> end</div><div> object MySQL55Connection1: TMySQL55Connection</div><div> Connected = False</div><div> LoginPrompt = False</div><div> DatabaseName = 'test'</div><div> KeepConnection = False</div><div> Password = 'root'</div><div> Transaction = SQLTransaction1</div><div> UserName = 'root'</div><div> HostName = '127.0.0.1'</div><div> LogEvents = []</div><div> left = 40</div><div> top = 16</div><div> end</div><div> object SQLQuery1: TSQLQuery</div><div> FieldDefs = <></div><div> Database = MySQL55Connection1</div><div> Transaction = SQLTransaction1</div><div> Params = <></div><div> left = 88</div><div> top = 116</div><div> end</div><div> object SQLTransaction1: TSQLTransaction</div><div> Active = False</div><div> Database = MySQL55Connection1</div><div> left = 147</div><div> top = 45</div><div> end</div><div>end</div></div><div><br></div><div>...</div><div><br></div><div>unit1.pas:</div><div><br></div><div><div>unit Unit1;</div><div><br></div><div>{$mode objfpc}{$H+}</div><div><br></div><div>interface</div><div><br></div><div>uses</div><div> SysUtils, mysql55conn, sqldb, Forms, Dialogs, StdCtrls;</div><div><br></div><div>type</div><div><br></div><div> { TForm1 }</div><div><br></div><div> TForm1 = class(TForm)</div><div> Button1: TButton;</div><div> MySQL55Connection1: TMySQL55Connection;</div><div> SQLQuery1: TSQLQuery;</div><div> SQLTransaction1: TSQLTransaction;</div><div> procedure Button1Click(Sender: TObject);</div><div> end;</div><div><br></div><div>var</div><div> Form1: TForm1;</div><div><br></div><div>implementation</div><div><br></div><div>{$R *.lfm}</div><div><br></div><div>{ TForm1 }</div><div><br></div><div>procedure TForm1.Button1Click(Sender: TObject);</div><div>begin</div><div> SQLQuery1.SQL.Text := 'insert into test (foo) values (:foo)';</div><div> SQLQuery1.Params.ParamByName('foo').AsString := DateTimeToStr(Now);</div><div> SQLQuery1.ExecSQL;</div><div> ShowMessageFmt('%d', [MySQL55Connection1.GetInsertID]);</div><div>end;</div><div><br></div><div>end.</div></div><div><br></div><div>Result after three clicks:</div><div><br></div><div><div>[Window Title]</div><div>project1</div><div><br></div><div>[Content]</div><div>4</div><div><br></div><div>[OK]</div></div><div><br></div><div>SQL:</div><div><br></div><div><div>-- phpMyAdmin SQL Dump</div><div>-- version 4.2.11</div><div>-- <a href="http://www.phpmyadmin.net">http://www.phpmyadmin.net</a></div><div>--</div><div>-- Host: localhost</div><div>-- Generation Time: 14-Nov-2014 às 17:09</div><div>-- Versão do servidor: 5.5.40</div><div>-- PHP Version: 5.3.21</div><div><br></div><div>SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";</div><div>SET time_zone = "+00:00";</div><div><br></div><div>--</div><div>-- Database: `test`</div><div>--</div><div><br></div><div>-- --------------------------------------------------------</div><div><br></div><div>--</div><div>-- Estrutura da tabela `test`</div><div>--</div><div><br></div><div>CREATE TABLE IF NOT EXISTS `test` (</div><div>`id` int(11) NOT NULL,</div><div> `foo` varchar(20) NOT NULL</div><div>) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;</div><div><br></div><div>--</div><div>-- Indexes for dumped tables</div><div>--</div><div><br></div><div>--</div><div>-- Indexes for table `test`</div><div>--</div><div>ALTER TABLE `test`</div><div> ADD PRIMARY KEY (`id`);</div><div><br></div><div>--</div><div>-- AUTO_INCREMENT for dumped tables</div><div>--</div><div><br></div><div>--</div><div>-- AUTO_INCREMENT for table `test`</div><div>--</div><div>ALTER TABLE `test`</div><div>MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;</div><div><br></div></div>-- <br><div class="gmail_signature">Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a></div>
</div></div>