PHP Doku:: Liefert die Zeilenidentifikation der zuletzt eingefügten Reihe zurück - function.sqlite-last-insert-rowid.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenSQLiteSQLite Funktionensqlite_last_insert_rowid -- SQLiteDatabase->lastInsertRowid

Ein Service von Reinhard Neidl - Webprogrammierung.

SQLite Funktionen

<<sqlite_last_error -- SQLiteDatabase->lastError

sqlite_libencoding>>

sqlite_last_insert_rowid

SQLiteDatabase->lastInsertRowid

(PHP 5, PECL sqlite >= 1.0.0)

sqlite_last_insert_rowid -- SQLiteDatabase->lastInsertRowidLiefert die Zeilenidentifikation der zuletzt eingefügten Reihe zurück

Beschreibung

int sqlite_last_insert_rowid ( resource $dbhandle )

Objektorientierter Stil (Methode):

int SQLiteDatabase::lastInsertRowid ( void )

Liefert die Zeilenidentifikation der zuletzt in die Datenbank dbhandle eingefügten Reihe zurück, wenn diese als automatische hochzählendes ('auto-increment') Feld angelegt wurde.

Tipp

In SQLite erzeugt man ein 'auto-increment'-Feld in dem man dieses als INTEGER PRIMARY KEY im Tabellen-Schema definiert.

Parameter-Liste

dbhandle

Die SQLite-Datenbankressource, zurückgegeben von sqlite_open(), wenn prozedural angewendet. Wird die objektorientierte Notation genutzt, wird dieser Parameter nicht benötigt.

Rückgabewerte

Liefert die Kennung der Zeilen als Integer.


3 BenutzerBeiträge:
- Beiträge aktualisieren...

22.02.2006 17:31
Note that you don't need to create a primary key for your table. In the absence of an integer primary key, SQLite simply uses an internal key called "RowID" or "OID".
It has all of the features of a normal integer primary key(auto-incrementing, returned by sqlite_last_insert_rowid(), accessible via SELECT, etc), except that it's handled better by SQLite and doesn't require the extra characters in the "CREATE" command.
If you just want a primary key as an auto-incrementing ID for your rows, save yourself the trouble and just use "OID" instead.

Also, according to the SQLite website, the id returned is for the last insert in the current *connection*, not just database. This means that if there are two connections to the same database at the same time, and both perform "INSERT", each can get it's own inserted rowid later, so basically this is thread-safe(So long as no-one calls "REINDEX").
ffoeg at shaw dot ca
2.01.2006 2:53
Note that if you insret a few rows in your table, say rowid 1-5, and then delete 2,3 and 4, you will be left with the rowids 1 and 5, not 1 and 2. This may seem basic to anyone with database experience, but I had to check it out with a GUI tool before I figured this one out. Anyway, what it boils down to is that sometimes your rowid will be greater than the number returned by the SQLite COUNT (*) function.
David
16.04.2005 4:50
I have noticed that this will still return an insert id even if a table has not been assigned a primary key or has not been assigned a primary key correctly. If the table was not created correctly sqlite_last_insert_rowid() will return its internal row id, which is probably not what you want. The correct way to establish a primary key migh look something like this look something like this:

create table some_table (id INTEGER PRIMARY KEY, some_field varchar(10))

Then you can try:

insert into some_table (some_field) values ('bob')

You will see that an id will be automatically generated and
calling sqlite_last_insert_rowid() will return the expected value



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)