Does InsertID works for MyTable?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Willo
Posts: 34
Joined: Thu 24 Aug 2006 18:29

Does InsertID works for MyTable?

Post by Willo » Wed 20 Jun 2007 14:25

Hi;

im trying to obtain the last id after an Mytable.Append...Mytable.Post;

but MyTable.InsertId returns 0 every time.


Any Advice?

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 21 Jun 2007 09:51

Please make sure that your table has a columns marked as AUTO_INCREMENT. If it has, specify what options of MySQL or MyDAC have you changed. Also provide a piece of code that you use to append records.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 21 Jun 2007 10:58

Also specify what MyDAC version do you use.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 21 Jun 2007 12:18

Values for the fields marked as AUTO_INCREMENT are automatically obtained from the server, and reflected in the dataset field if the SQLInsert property is empty.

Poerenstamper
Posts: 4
Joined: Thu 04 Oct 2007 10:36

Post by Poerenstamper » Thu 04 Oct 2007 10:45

I'm afraid I'm having some trouble with 'InsertID' too. Some data:
- MyDAC 5 Pro
- MySQL 5.0.38
- Delphi 7

I use the TMyCommand to insert a records, in the following way:

Code: Select all

with MyCommand1 do
begin
  SQL.Clear;
  SQL.Add('INSERT INTO mailings (date) VALUES (NOW())');
  Prepare;
  Execute;
end;
The scheme of this table 'mailings' is (ID is an AUTO_INCREMENT field):

Code: Select all

Mailings(ID, date, sent);
In the event handler MyCommand1AfterExecute I have:

Code: Select all

if Result then
  MailingID := MyCommand1.InsertId;
Now the problem: the first I insert a record, MailingID remains the default value (0), however the second time it works just fine.

I tried several things, like refreshes and so on, but the result stays the same.

Thanks in advance for your answers!

Wessel
Last edited by Poerenstamper on Thu 04 Oct 2007 12:49, edited 1 time in total.

Poerenstamper
Posts: 4
Joined: Thu 04 Oct 2007 10:36

Post by Poerenstamper » Thu 04 Oct 2007 12:48

Poerenstamper wrote:..however the second time it works just fine.
Actually, that was a bit too optimistic; every n'th time the (n-1)'th ID is retrieved. So the second time, the inserted ID from the first try is returned.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 05 Oct 2007 12:15

Thank you for information. We have reproduced this problem. The investigation of the problem is in progress. As soon as we solve it, we will let you know.

Poerenstamper
Posts: 4
Joined: Thu 04 Oct 2007 10:36

Post by Poerenstamper » Fri 05 Oct 2007 12:29

Thank you very much!

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Fri 19 Oct 2007 12:36

We have fixed this problem. This fix will be included in the next MyDAC build. Please watch for announcements at the forum.

Poerenstamper
Posts: 4
Joined: Thu 04 Oct 2007 10:36

Post by Poerenstamper » Fri 19 Oct 2007 12:40

Very nice, thanks!

Post Reply