Board index » delphi » mySQL and auto_increment values
Grant Boggs
![]() Delphi Developer |
Sun, 23 May 2004 09:45:45 GMT
|
Grant Boggs
![]() Delphi Developer |
Sun, 23 May 2004 09:45:45 GMT
mySQL and auto_increment values
After inserting a value into a table with an auto_increment field type,
how do I retrieve the value for use in my program??? |
Team
![]() Delphi Developer |
Mon, 24 May 2004 04:17:43 GMT
Re:mySQL and auto_increment valuesQuote"Ramesh Theivendran (BORLAND)" wrote: -Craig -- |
BORLA
![]() Delphi Developer |
Mon, 24 May 2004 04:10:28 GMT
Re:mySQL and auto_increment valuesTry setting DatasetProvider option poAutoRefresh and i see folks have reported that its not working correctly. Another workaround would be to refresh the whole dataset and that will get the newly inserted auto increment values. This may be expensive in some cases where you are dealing with large resultset or improper filtering in the SQL. T.Ramesh. QuoteGrant Boggs wrote: This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately and permanently delete the original and any copy of any e-mail and any printout thereof. Accelerate your Linux? application development with Kylix(TM), the RAD |
David People
![]() Delphi Developer |
Mon, 24 May 2004 05:50:20 GMT
Re:mySQL and auto_increment valuesQuoteGrant Boggs wrote: select last_insert_id() from [anytablename] limit 1 'anytablename' can be any existing table in the database, not necessarily MySQL maintains a unique last id value for each established connection. So In Delphi 5 using the MySQL odbc driver the query was much cleaner, simply I haven't tested any of this in Kylix 2 or Delphi 6 yet. David -- |
Grant Bogg
![]() Delphi Developer |
Mon, 24 May 2004 13:02:42 GMT
Re:mySQL and auto_increment valuesQuote"David Peoples" <dav...@touringcyclist.com> wrote in message Quote> You can run this query to retrieve the value: |
Ross Davis - DataAnywhere.ne
![]() Delphi Developer |
Mon, 24 May 2004 14:54:15 GMT
Re:mySQL and auto_increment valuesI turned on detailed logging on my Mysql server and found that transactions are being attempted, but every sql statement that is sent to the server is sent as a seperate connection!! This is causing the all kinds of problems. The biggest for me, is that there is no way to even get the id of the last record that was just inserted even from the server. I did get it to work with keepconnection=false, but I have to have keepconnection=true in order for master detail to work. I am using datasnap with and the provider is doing funky things with I am going to have to implement some other type of id generation routine to I am going to try to do an insert, get last_insert_id() and delete where |
Ross Davis - DataAnywhere.ne
![]() Delphi Developer |
Mon, 24 May 2004 14:56:14 GMT
Re:mySQL and auto_increment valuesThat works unless you are using datasnap. Then it returns 0 all the time. (Unless you only have one table connected to the provider and keepconnection=false) |
Thomas Kaltschmid
![]() Delphi Developer |
Mon, 24 May 2004 17:17:52 GMT
Re:mySQL and auto_increment valuesWhy not use mySQL-function "LAST_INSERT_ID()" and you can get the automatically created id directly after creation. Thomas Am 05.12.2001 21:10 Uhr schrieb "Ramesh Theivendran (BORLAND)" unter Quote> Try setting DatasetProvider option poAutoRefresh and i see folks have reported |
Ross Davis - DataAnywhere.ne
![]() Delphi Developer |
Mon, 24 May 2004 17:13:28 GMT
Re:mySQL and auto_increment valuesbecause of a bug in either the datasnap provider or the dbexpress/Mysql driver the value is being wiped out. I Traced my Mysql logs and found that a new connection to the db is occuring for every sql statement that is sent to the server. Since it is a new connection it can't give you the last_insert_id because it may not of belonged to you. |
David People
![]() Delphi Developer |
Wed, 26 May 2004 02:40:52 GMT
Re:mySQL and auto_increment valuesRoss Davis - DataAnywhere.net wrote: Quote> because of a bug in either the datasnap provider or the dbexpress/Mysql (trial version) using a TSqlQuery. Don't know yet if it works in a TSqlClientDataset. Arrange for this code to run just after the insertion query executes. The David -- |
David People
![]() Delphi Developer |
Wed, 26 May 2004 04:47:44 GMT
Re:mySQL and auto_increment valuesQuoteDavid Peoples wrote: a TSQLQuery.ExecSQL, just after TSQLQuery.Open. Below is code for a component that saves the last auto_increment value. It --- Code starts here --- unit MySQLAutoIncQuery; interface uses type procedure Register; implementation constructor TMySQLAutoIncQuery.Create(AOwner: TComponent); destructor TMySQLAutoIncQuery.Destroy; function TMySQLAutoIncQuery.ExecSQL(ExecDirect: Boolean = False): Integer; procedure Register; end. --- Code ends here --- -- |
Ross Davis - DataAnywhere.ne
![]() Delphi Developer |
Wed, 26 May 2004 10:50:37 GMT
Re:mySQL and auto_increment valuesbecause of the numerous bugs in datasnap with dbexpress, I had to create these functions and set the record id's of new records in the beforeupdaterecord. (Sample below) Once the bugs have been fixed I will be able to check the last_insert_id in the afterupdaterecord function instead of before Note: I use a similar routine to what Dan Miser suggested for this where new These routines work great and with poPropigatechaanges=true the updates go // AUV = After Update Value function TEraDataServer.getid(table,field:string) : integer; Function TEraDataServer.CheckID(var Deltads : TCustomClientDataset; Function TEraDataServer.CheckMasterID(var Deltads : TCustomClientDataset; procedure TEraDataServer.master_ProviderBeforeUpdateRecord(Sender: TObject; if SourceDS = detail_SQLDS then if SourceDS = sub_detail_SQLDS then end; |