Get rowID of record being INSERTED into SQLite database
Get rowID of record being INSERTED into SQLite database
When I INSERT a new record into an SQLite database, is there a way to get the rowID of that record?
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
- NicTheQuick
- Addict
- Posts: 1227
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Get rowID of record being INSERTED into SQLite database
Code: Select all
select last_insert_rowid();
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Re: Get rowID of record being INSERTED into SQLite database
Thank you but, could you please give me an idea as to where in my database query I would put that?
My query is:
Thanks.
My query is:
Code: Select all
result = DatabaseUpdate(#dbaseID, "INSERT INTO command " +
"(category, cmd, syntax) " +
"VALUES (?, ?, ?);")
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
- NicTheQuick
- Addict
- Posts: 1227
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Get rowID of record being INSERTED into SQLite database
Just call it after your update. I never made anything with databases in Purebasic I guess it should look something like this:
Code: Select all
DatabaseQuery(#dbaseID, "select last_insert_rowid();"
If NextDatabaseRow(#dbaseID)
Debug GetDatabaseLong(#dbaseID, 0)
EndIf
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Re: Get rowID of record being INSERTED into SQLite database
That works. Thank you so much.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Re: Get rowID of record being INSERTED into SQLite database
Alternatively you can get it first and then use it in your insert statement [either way, you have two DB round trips ]:
Code: Select all
Procedure.l GetNextAutoIncIDForTable(DatabaseHandle, TableName.s)
Protected expr.s = "select seq + 1 as NextID from sqlite_sequence where lower(name) = lower(" + QuotedStr(TableName.s) + ")", result = -1
If DatabaseQuery(DatabaseHandle, expr)
If NextDatabaseRow(DatabaseHandle)
Result = GetDatabaseLong(DatabaseHandle, 0)
Else ; no entry in sequence table yet
Result = 1
EndIf
FinishDatabaseQuery(DatabaseHandle)
Else
; handle query error here
EndIf
ProcedureReturn result
EndProcedure
PB 5.73 on Windows 10 & OS X High Sierra
-
- Enthusiast
- Posts: 501
- Joined: Tue Oct 14, 2014 12:09 pm
Re: Get rowID of record being INSERTED into SQLite database
Here is my answerColumbo wrote:When I INSERT a new record into an SQLite database, is there a way to get the rowID of that record?
The rowid of the last inserted row can be selected with the last_insert_rowid() function:
Here is an example in sql
Code: Select all
UseSQLiteDatabase()
If FileSize(GetTemporaryDirectory()+"toto.db") <> -1 And FileSize(GetTemporaryDirectory()+"toto.db") <> -2
DeleteFile(GetTemporaryDirectory()+"toto.db")
EndIf
If FileSize(GetTemporaryDirectory()+"toto.db") = -1
CreateFile(0, GetTemporaryDirectory()+"toto.db", #PB_UTF8 )
CloseFile(0)
EndIf
Global ouverture.l =OpenDatabase(#PB_Any, GetTemporaryDirectory()+"toto.db" , "", "", #PB_Database_SQLite )
Requete.s = "CREATE TABLE Cats( CatId INTEGER PRIMARY KEY, CatName);INSERT INTO Cats VALUES ( NULL, 'Brush' ),( NULL, 'Scarcat' ), ( NULL, 'Flutter' )"
DatabaseUpdate(ouverture,requete)
;====================================
; This instruction after an insert give the result you search
;====================================
DatabaseQuery(ouverture,"Select last_insert_rowid();")
NextDatabaseRow(ouverture)
Debug GetDatabaseLong(ouverture,0)
FinishDatabaseQuery(ouverture)
CloseDatabase(ouverture)