How to return the value of AUTO INCREMENT column in SQLite with VB6
Asked Answered
T

6

14

I have a table in SQLite:

CREATE TABLE "EventType" 
(
[EventTypeID] INTEGER PRIMARY KEY, 
[EventTypeName] VARCHAR(50) NOT NULL UNIQUE
);

Since EventTypeID is an integer and a primary key, that automatically makes it an auto-incrementing column, and that works fine.

I'd like to insert a row into the table and get the newly incremented value from VB6.

Dim oRs as Recordset
dim oCmd as new Command

oCmd.ActiveConnection = GetConnection()
oCmd.Source = "insert into EventType (EventTypeName) values ('blah')"
oCmd.Execute

Is there an automatic way to retrieve the newly created EventTypeID without having to issue another query (select max(EventTypeID) from EventType))?

I seem to remember from VB6 days long time ago, that there was a way to do that.

Topee answered 10/2, 2009 at 5:3 Comment(1)
Possible duplicate of Does SQLite support SCOPE_IDENTITY?Ician
L
17

Does SQLite support SCOPE_IDENTITY?

Check out the FAQ. The sqlite3_last_insert_rowid() function will do it. Careful of triggers though

Not tested, but you should be able to send both statements in one call. It's been a while since I wrote any VB6. Also this is not SQL injection safe.

Dim oRs as Recordset
dim sSql as String
sSql = "INSERT INTO EventType (EventTypeName) VALUES ('blah'); SELECT last_insert_rowid() FROM EventType"
oRs.Open sSql oConn
Lasandralasater answered 10/2, 2009 at 5:15 Comment(2)
btw, with the ODBC driver, you have to split your query into 2 calls.Topee
so what does Insert() return then? - I know I can look it up but it might be useful here for completeness, since that's how I found this post.Citrine
P
13

Run this query inside of your code:

SELECT SEQ from sqlite_sequence WHERE name='tablename'
Padlock answered 19/11, 2015 at 4:24 Comment(0)
L
3

I don't use VB, so I don't know the interface you're using with your database, but, as bendewey said, there is a function in the c API called sqlite3_last_insert_rowid() that will return the primary integer key for the last inserted row, so you could look for a similar function in your interface.

If that doesn't work, you can use the SQLite-specific query:

SELECT last_insert_rowid()
Lyns answered 10/2, 2009 at 5:20 Comment(0)
F
2

You can call SELECT last_insert_rowid() after your insert

Frottage answered 10/2, 2009 at 5:21 Comment(0)
F
1

Since Version 3.35 you can use

Insert Into <yourtable> Default Values Returning <idcolumn>

Run the Insert with ExecuteScalar.

Fenestration answered 15/12, 2023 at 12:4 Comment(0)
N
-1

If use

SELECT last_insert_rowid() 

returns the last rowid, you can use :

SELECT last_insert_rowid() AS rowid FROM table_name LIMIT 1

for obtain the last rowid for an individual table

Nth answered 20/3, 2014 at 8:15 Comment(1)
This not what the documentation says: "The last_insert_rowid() SQL function is a wrapper around the sqlite3_last_insert_rowid() C/C++ interface function." which means that there is no such thing as the last rowid for an individual table.Louisville

© 2022 - 2024 — McMap. All rights reserved.