Hi, Sorry my english first of all, i'm brazilian and my english is very poor lol
I Need discover what is the value of Auto Increment, i use this code in PHP:
$sqlid = "SHOW TABLE STATUS LIKE \"pira_imob\"";
$exeid = mysql_query($sqlid,$base)or die (mysql_error());
$arrid = mysql_fetch_array($exeid);
$idimo= $arrid['Auto_increment'];
I Try make something like this:
SQLstring.Add('SHOW TABLE STATUS LIKE "vendas"');
frmprincipal.Zcomprabusca.SQL.Clear;
frmprincipal.Zcomprabusca.SQL:= SQLstring;
frmprincipal.Zcomprabusca.ExecSQL;
IDa:= frmprincipal.Zcomprabusca.DbcResultSet.GetStringByName('Auto_increment');
But don't Work, show-me this exception appointing at line IDa:= :
Acess Violation at adress 0058350E in module 'soft.exe' Read of adress 0000000
Please Help-me, i need this so much!
Thanks
How to Discover Next AutoIncrement Number
Moderators: gto, cipto_kh, EgonHugeist
-
- Fresh Boarder
- Posts: 4
- Joined: 01.06.2008, 05:33
- mdaems
- Zeos Project Manager
- Posts: 2766
- Joined: 20.09.2005, 15:28
- Location: Brussels, Belgium
- Contact:
Depends on what you need it for...
- Just to fill the number when inserting rows using a ZQuery component by using ZQuery.Append : You should not need it. Just insert all other fields. After ApplyUpdates the ID is filled.
- To know what row has last been inserted IN THE SAME CONNECTION : select last_insert_id();
- To know what the NEXT id will be? Forget it. If you need that use something you build yourself to build the autoincrement. You could go for 'select max(id) + 1 from table', but that's not safe when multiple connections can be made to the same database.
Some remark on your code :
- If you want a resultset you should use ZQuery.Open instead of ExecSQL. (that's more useful for Update statements)
- To get the result data (in case of Open) : ZQuery1.FieldByName('NAME').AsString
Mark
- Just to fill the number when inserting rows using a ZQuery component by using ZQuery.Append : You should not need it. Just insert all other fields. After ApplyUpdates the ID is filled.
- To know what row has last been inserted IN THE SAME CONNECTION : select last_insert_id();
- To know what the NEXT id will be? Forget it. If you need that use something you build yourself to build the autoincrement. You could go for 'select max(id) + 1 from table', but that's not safe when multiple connections can be made to the same database.
Some remark on your code :
- If you want a resultset you should use ZQuery.Open instead of ExecSQL. (that's more useful for Update statements)
- To get the result data (in case of Open) : ZQuery1.FieldByName('NAME').AsString
Mark
-
- Fresh Boarder
- Posts: 4
- Joined: 01.06.2008, 05:33
How should i use the last_insert_id(), how do i put the result in a variable?
i tryed this code: ZQuery1.FieldByName('NAME').AsString but a error message appears saying that field doesn't exists
CODE:
frmprincipal.ZID.SQL.add('SELECT last_insert_id() as IDfinal');
frmprincipal.ZID.Open;
frmprincipal.ZID.ExecSQL;
IDint:= frmprincipal.ZID.fieldbyname('IDfinal').AsInteger;
Thanks
i tryed this code: ZQuery1.FieldByName('NAME').AsString but a error message appears saying that field doesn't exists
CODE:
frmprincipal.ZID.SQL.add('SELECT last_insert_id() as IDfinal');
frmprincipal.ZID.Open;
frmprincipal.ZID.ExecSQL;
IDint:= frmprincipal.ZID.fieldbyname('IDfinal').AsInteger;
Thanks
Last edited by Marcos-N41 on 01.06.2008, 23:18, edited 1 time in total.
- mdaems
- Zeos Project Manager
- Posts: 2766
- Joined: 20.09.2005, 15:28
- Location: Brussels, Belgium
- Contact:
First, read http://dev.mysql.com/doc/refman/5.0/en/ ... -insert-id
Then (not tested, however!):
Mark
Then (not tested, however!):
Code: Select all
ZQuery1.SQL.Text := 'Select last_insert_id() as lastinsertid;';
ZQuery1.Open;
ShowMessage(IntToStr(ZQuery1.GetFieldByName('lastinsertid').asInteger));
ZQuery1.Close;
-
- Fresh Boarder
- Posts: 4
- Joined: 01.06.2008, 05:33
i tryed this code:
ZQuery1.FieldByName('NAME').AsString but a error message appears saying that field doesn't exists
CODE:
frmprincipal.ZID.SQL.add('SELECT last_insert_id() as IDfinal');
frmprincipal.ZID.Open;
frmprincipal.ZID.ExecSQL;
IDint:= frmprincipal.ZID.fieldbyname('IDfinal').AsInteger;
Thanks
ZQuery1.FieldByName('NAME').AsString but a error message appears saying that field doesn't exists
CODE:
frmprincipal.ZID.SQL.add('SELECT last_insert_id() as IDfinal');
frmprincipal.ZID.Open;
frmprincipal.ZID.ExecSQL;
IDint:= frmprincipal.ZID.fieldbyname('IDfinal').AsInteger;
Thanks
-
- Fresh Boarder
- Posts: 4
- Joined: 01.06.2008, 05:33