Hallo
Mal wieder eine Frage @ SQL(lite)
ich füge mit einem Insert Befehl ein Eintrag hinzu
INSERT INTO Test (Datum, value, text) VALUES(date('2006-03-03'), '75', 'TEEXT')
Die Struktur der Tabelle Test ist
---Test----
- ID -
- Datum -
- value -
- text -
------------
Nun bräuchte ich gleich die ID des neuen Eintrages.
Problem ich kann nicht nach Datum + value text fragen, weil durchaus "doppeleinträge" möglich wären
Könnte man vom Insert gleich die ID abfragen?
Thx
dm
SQL Von insert ID holen?
- DatenMetzgerX
- User
- Beiträge: 398
- Registriert: Freitag 28. April 2006, 06:28
- Wohnort: Zürich Seebach (CH)
Das hab ich jetzt auf die schnelle jetzt auch nicht auswendig gewusst aber es gibt ja google
Inserting data, and a little SQLite trick
[...]
You see, SQLite doesn't technically do PRIMARY KEY AUTOINCREMENT. But, if you create an INTEGER PRIMARY KEY column, and insert NULL into it, it will increment one higher than the highest number in that column. Make sense?
TIP: You can get the integer key used for the last insert from your cursor object with lastrowid.
Now, there's a security issue in insert statements, which I'll cover shortly. Read on!
----------------------------
MfG
Andreas
P.S Nach -Allgemeine Fragen- verschoben - Ach verdammt kann ich ja nicht *g*
... geht ja doch! merkt man das ich lange nicht hier war
Inserting data, and a little SQLite trick
[...]
You see, SQLite doesn't technically do PRIMARY KEY AUTOINCREMENT. But, if you create an INTEGER PRIMARY KEY column, and insert NULL into it, it will increment one higher than the highest number in that column. Make sense?
TIP: You can get the integer key used for the last insert from your cursor object with lastrowid.
Code: Alles auswählen
>>> cur.execute('INSERT INTO foo (o_id, fruit, veges) VALUES(NULL, "apple", "broccoli")')
>>> con.commit()
>>> cur.lastrowid
1
>>> con.close()
----------------------------
MfG
Andreas
P.S Nach -Allgemeine Fragen- verschoben - Ach verdammt kann ich ja nicht *g*
