Get rowID of record being INSERTED into SQLite database

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Get rowID of record being INSERTED into SQLite database

Post by Columbo »

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.
User avatar
NicTheQuick
Addict
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

Post by NicTheQuick »

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.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Get rowID of record being INSERTED into SQLite database

Post by Columbo »

Thank you but, could you please give me an idea as to where in my database query I would put that?

My query is:

Code: Select all

result = DatabaseUpdate(#dbaseID, "INSERT INTO command " +
                                    "(category, cmd, syntax) " +
                                    "VALUES (?, ?, ?);")
Thanks.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
NicTheQuick
Addict
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

Post by NicTheQuick »

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.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Get rowID of record being INSERTED into SQLite database

Post by Columbo »

That works. Thank you so much. :D
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Get rowID of record being INSERTED into SQLite database

Post by kpeters58 »

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
loulou2522
Enthusiast
Enthusiast
Posts: 501
Joined: Tue Oct 14, 2014 12:09 pm

Re: Get rowID of record being INSERTED into SQLite database

Post by loulou2522 »

Columbo wrote:When I INSERT a new record into an SQLite database, is there a way to get the rowID of that record?
Here is my answer

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)
Post Reply