Hi... this is the complete code I'm using to extract the last_insert_id from the table TABLE1, but it still don't works. Infact I get always 0 from the variable "id_ingresso", pheraphs in the database the row is correctly inserted.

Maybe it's a bug of the component?
--------------------------------------------------------------------------------------------------------------------------------
This is the structure of the table:

CREATE TABLE `TABLE1`
(
`PK_TABLE1` integer (11) UNSIGNED  NOT NULL AUTO_INCREMENT ,
`FIELD1`varchar (20),
`FIELD2`varchar (20),
`FIELD3`datetime NOT NULL,
PRIMARY KEY (`PK_TABLE1`)
) TYPE=MyISAM

--------------------------------------------------------------------------------------------------------------------------------
This is the code:

procedure TPassaggioGestito.RegistraPassaggioSulDB(Connessione: TSQLConnection);
var
 Qry: TSQLQuery;
 Trans: TSQLTransaction;
begin

 //parametri db per query
 Trans:=TSQLTransaction.Create(nil);
 Qry:=TSQLQuery.Create(nil);
 try
  Connessione.Transaction := Trans;
  Qry.DataBase := Connessione;
  Qry.Transaction := Trans;

  Connessione.StartTransaction;

  Qry.ParseSQL := true;
  Qry.ReadOnly := false;


  Qry.Active := false;
  Qry.SQL.Text := 'INSERT INTO TABLE1  '+
                  '(FIELD1, FIELD2, FIELD3) '+
                  'VALUES '+
                  '(:FIELD1, :FIELD2, :FIELD3) ';
  Qry.Params.ParamByName('FIELD1').AsString := self.field1;
  Qry.Params.ParamByName('FIELD2').AsString := self.field2;
  Qry.Params.ParamByName('FIELD3').AsDateTime := self.field3;

  try
     Qry.ExecSQL;
  except
Log.Scrivi(0,'Errore SQL in passaggigestiti: SQL = "'+qry.SQL.Text+'"');
  end;

  //la query viene ora usata in lettura
  Qry.Active := false;
  qry.ParseSQL := false;
  qry.ReadOnly := true;

  //leggo il pk_ingresso appena inserito
  Qry.SQL.Text := 'SELECT LAST_INSERT_ID() as PRIMARY_KEY';
  try
    Qry.open;
    self.id_ingresso := Qry.FieldByName('PRIMARY_KEY').AsInteger;
    Qry.close;
  except
Log.Scrivi(0,'Errore SQL in passaggigestiti: SQL = "'+Qry.SQL.Text+'"');
     self.id_ingresso := -1;
  end;

 finally
  Connessione.EndTransaction;
  Qry.Free;
  Trans.Free;
 end;

end;
--------------------------------------------------------------------------------------------------------------------------------

_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to