Autor Beitrag
DarkHunter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 477

Win XP
D3 Prof, D2005 PE
BeitragVerfasst: Mo 11.12.06 18:19 
Hallo,

ich habe eine kleines Problem. Ich füge einen Eintrag in eine Datenbank ein, die einzige eindeutige Identifikation ist der Primärschlüssel ID. Dieser wird automatisch erhöht.
Gibt es jetzt ein Möglichkeit, die ID des soeben über INSERT INTO erstellten Eintrags zu erhalten?

_________________
I believe that every human has a finite number of heart-beats. I don't intend to waste any of mine running around doing exercises.
- Neil Armstrong
stifflersmom
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194

XP /XP PRO/ SuSE div.
D1 - D7, BDS 2006
BeitragVerfasst: Mo 11.12.06 19:14 
eine simple Methode ist, diesen Wert über eine Query zu machen.
Gehn wir davon aus, dass immer der letzte datensetz der neueste ist,
kannst Du das also so machen:
select id from meine Tabelle order by id desc limit 1;die feldnamen musst Du natürlich anpassen...

Moin
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Mo 11.12.06 19:34 
So hatte ich das auch mal gemacht und meine Erfahrung war, dass nachdem man einen Eintrag gelöscht hat und danach einen neu erstellt hat, der neue die ID des gelöschten bekommen hat (MySql, die Version die bei Suse Linux 9.3 dabei ist)

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
DarkHunter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 477

Win XP
D3 Prof, D2005 PE
BeitragVerfasst: Mo 11.12.06 20:20 
Und jetzt sage mir bitte einer warum, ich nicht selber auf diese eigentlich banale Lösung gekommen bin?

Danke, das sollte funktionieren.

_________________
I believe that every human has a finite number of heart-beats. I don't intend to waste any of mine running around doing exercises.
- Neil Armstrong
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Mo 11.12.06 20:36 
Da aus dem ersten Beitrag hervorgeht, das er auto. hochgezählt wird, handelt es sich um einen Integer also würde ich

ausblenden SQL-Anweisung
1:
Select max(ID) from meineTabelle;					


machen.

LG
Stefan

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.
Blawen
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 616
Erhaltene Danke: 33

Win XP, Vista, 7
Delphi 5 Prof., BDS 2006 Prof. RAD Studio XE
BeitragVerfasst: Mo 11.12.06 23:22 
user profile iconDarkHunter hat folgendes geschrieben:
Gibt es jetzt ein Möglichkeit, die ID des soeben über INSERT INTO erstellten Eintrags zu erhalten?


MySQL bietet von Haus eine Lösung an:
ausblenden SQL-Anweisung
1:
'Select LAST_INSERT_ID()'					


So kann es z.B. eingesetzt werden:
ausblenden Delphi-Quelltext
1:
2:
3:
ZQ.SQL.Text := 'Select LAST_INSERT_ID()';
ZQ.Active   := True;
iHilfs      := ZQ.Fields[0].AsInteger;

_________________
Es kompilert, wir können ausliefern.
Und es kompiliert wieder - das Update ist fertig - bitte 100 Euro ;-)