Recent

Author Topic: Why does GetInsertID always return 0?  (Read 2169 times)

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Why does GetInsertID always return 0?
« on: January 11, 2019, 02:36:14 am »
Hi, guys,

I'm using TMySQL56Connection, connect to a MariaDB server,  which version is 10.2.18-MariaDB-10.2.18+maria~xenial-log.
Everything is OK except when I trying to get last insert id.

I insert a record, then if I using a query 'select last_insert_id()', the result is correct.
But if I use TConnectionName(TheQuery.Database).GetInsertID, it returns 0.
What's wrong?

« Last Edit: January 11, 2019, 03:35:59 am by kinlion »

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Why does GetInsertID always return 0?
« Reply #1 on: January 16, 2019, 10:19:45 am »
this is an assumption. But if Last_ID is auto generated it is also the Max_ID.
Can't you try and get that one?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Why does GetInsertID always return 0?
« Reply #2 on: January 17, 2019, 10:38:29 am »
I dig in the implementation, it seems correct, but I'm on trunk. No idea if it was already implemented in stable.
Code: Pascal  [Select][+][-]
  1. // $(fpcsrc)/packages/fcl-db/src/sqldb/mysql/mysqlconn.inc:378
  2. Function TConnectionName.GetInsertID: int64;
  3. begin
  4.   CheckConnected;
  5.   Result:=mysql_insert_id(GetHandle);
  6. end;
  7.  

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Re: Why does GetInsertID always return 0?
« Reply #3 on: January 18, 2019, 02:24:08 am »
this is an assumption. But if Last_ID is auto generated it is also the Max_ID.
Can't you try and get that one?

Max_ID maybe not what I want if multiple connections do Insert at same time.

kinlion

  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Re: Why does GetInsertID always return 0?
« Reply #4 on: January 18, 2019, 02:28:37 am »
I dig in the implementation, it seems correct, but I'm on trunk. No idea if it was already implemented in stable.
Code: Pascal  [Select][+][-]
  1. // $(fpcsrc)/packages/fcl-db/src/sqldb/mysql/mysqlconn.inc:378
  2. Function TConnectionName.GetInsertID: int64;
  3. begin
  4.   CheckConnected;
  5.   Result:=mysql_insert_id(GetHandle);
  6. end;
  7.  

Yes, I also see the implementation.
I think there must be some bad code in my project because I have used it before, and the result was right.
Now, I use 'select last_insert_id()'.
I will dig it when have time.

Thanks Leledumbo :)
« Last Edit: January 18, 2019, 02:30:21 am by kinlion »

 

TinyPortal © 2005-2018