![]() |
Datenbank: SQLite • Version: 3 • Zugriff über: Lazarus, SQLite3Connection
LastRowID wie auslesen?
Hallo,
ich möchte die letzte eingefügte ID auslesen, aber ich scheitere an der Syntax und brauche Hilfe. Hier mein Code:
Delphi-Quellcode:
Nun an welcher Stelle und vor allem wie lese ich diese aus?
procedure TForm2.InsertNewJob();
begin Form1.SQLQuery1.Close; Form1.SQLQuery1.SQL.Text := 'INSERT INTO tbmain VALUES(NULL, :title, :start, :ende, :memo, :fk_category_id, NULL, NULL, :fk_status_id, NULL)'; Form1.SQLQuery1.ParamByName('title').AsString := TitleEdit.Text; Form1.SQLQuery1.ParamByName('start').AsDateTime := StartDateEdit.Date; Form1.SQLQuery1.ParamByName('ende').AsDateTime := EndeDateEdit.Date; Form1.SQLQuery1.ParamByName('memo').AsString := Memo.Text; Form1.SQLQuery1.ParamByName('fk_category_id').AsInteger := Integer(CategoryComboBox.Items.Objects[CategoryComboBox.ItemIndex]); Form1.SQLQuery1.ParamByName('fk_status_id').AsInteger := 1; Form1.SQLQuery1.ExecSQL; Form1.SQLTransaction1.Commit; // Letze id auslesen Form1.SQLQuery1.Close; Form1.SQLQuery1.SQL.Text := 'SELECT last_insert_rowid() FROM tbmain'; Form1.SQLQuery1.Open; showmessage(inttostr(last_insert_rowid())); // funktioniert nicht end; |
AW: LastRowID wie auslesen?
|
AW: LastRowID wie auslesen?
Danke!
|
AW: LastRowID wie auslesen?
Eventuell kennt das SQLQuery1 oder die Connection auch eine LastInsertRowId-Methode.
|
AW: LastRowID wie auslesen?
Zitat:
Habe es jetzt wie folgt (siehe // Die letzte ID auslesen) gelöst. Aber die Lösung mit der LastRowID wird wohl mit anderen DBanken nicht funktionieren nehme ich an. Muss da wohl was anderes ausdenken, damit meine Anwendung auch mit MySQL später arbeiten kann.
Delphi-Quellcode:
procedure TForm2.InsertNewJob();
var CategoryLastID: Integer; begin Form1.SQLQuery1.Close; Form1.SQLQuery1.SQL.Text := 'INSERT INTO tbmain VALUES(NULL, :title, :start, :ende, :memo, :fk_category_id, NULL, NULL, :fk_status_id, NULL)'; Form1.SQLQuery1.ParamByName('title').AsString := TitleEdit.Text; Form1.SQLQuery1.ParamByName('start').AsDateTime := StartDateEdit.Date; Form1.SQLQuery1.ParamByName('ende').AsDateTime := EndeDateEdit.Date; Form1.SQLQuery1.ParamByName('memo').AsString := Memo.Text; // Kategorie nicht vorhanden und ComboBox nicht leer, also erst neue Kategorie in die DB schreiben if (CategoryComboBox.ItemIndex = -1) and (CategoryComboBox.Text <> '') and (CategoryComboBox.Items.IndexOf(CategoryComboBox.Text) = -1) then begin // String manuell reingeschrieben, String sonst in der CB nicht vorhanden // Kategorie in die Tabelle tbcategory schreiben Form1.SQLQueryCategory.Close; Form1.SQLQueryCategory.SQL.Text := 'INSERT INTO tbcategory VALUES(NULL, :category)'; Form1.SQLQueryCategory.ParamByName('category').AsString := CategoryComboBox.Text; Form1.SQLQueryCategory.ExecSQL; Form1.SQLTransaction1.Commit; // Die letzte ID auslesen Form1.SQLQueryCategory.Close; Form1.SQLQueryCategory.SQL.Text := 'SELECT last_insert_rowid() as id_tbcategory FROM tbcategory'; Form1.SQLQueryCategory.Open; CategoryLastID := Form1.SQLQueryCategory.FieldByName('id_tbcategory').AsInteger; Form1.SQLQuery1.ParamByName('fk_category_id').AsInteger := CategoryLastID; end else if (CategoryComboBox.ItemIndex = -1) and (CategoryComboBox.Items.IndexOf(CategoryComboBox.Text) <> -1) then begin // String manuell reingeschrieben, String in der CB vorhanden Form1.SQLQuery1.ParamByName('fk_category_id').AsInteger := Integer(CategoryComboBox.Items.Objects[CategoryComboBox.Items.IndexOf(CategoryComboBox.Text)]); end else if CategoryComboBox.ItemIndex <> -1 then begin // String in der CB ausgewählt Form1.SQLQuery1.ParamByName('fk_category_id').AsInteger := Integer(CategoryComboBox.Items.Objects[CategoryComboBox.ItemIndex]) end; Form1.SQLQuery1.ParamByName('fk_status_id').AsInteger := 1; Form1.SQLQuery1.ExecSQL; Form1.SQLTransaction1.Commit; end; Eine andere Frage... wenn ein Feld leer ist, muss ich in die DB ein NULL schreibe, oder reicht es wenn ich das Attribut gar nicht anfasse? |
AW: LastRowID wie auslesen?
Bei Parametern muss man vorsichtig sein, möglicherweise wird dann der Feld vom letzten Vorgang genommen. Also besser explizit auf NULL setzen.
|
AW: LastRowID wie auslesen?
Zitat:
Delphi-Quellcode:
Form1.SQLQuery1.ParamByName('fk_category_id').AsInteger := nil;
|
AW: LastRowID wie auslesen?
IIRC:
Delphi-Quellcode:
ParamByName('fk_category_id').Clear;
|
AW: LastRowID wie auslesen?
Oder so
Delphi-Quellcode:
, aber Clear ist schon schöner.
Form1.SQLQuery1.ParamByName('fk_category_id').Value := nil;
Zitat:
Da steht doch genau in der Fehlermeldung drin, was da nicht geht. Oder würdest du auch ein
Delphi-Quellcode:
oder
Form1.SQLQuery1.ParamByName('fk_category_id').AsInteger := 'abc';
Delphi-Quellcode:
veruchen?
Form1.SQLQuery1.ParamByName('fk_category_id').AsString := 123;
Eine weitere Frage ist aber auch: Warum ist der ganze Code in TForm2 drin, obwohl über 50% der Zugriffe auf TForm1 gehen? Das würde doch wohl eher in eine Methode der TForm1 gehören und die Werte z.B. als Parameter übergeben. :angel2: |
AW: LastRowID wie auslesen?
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:58 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz