How to return AUTO INCREMENT column value in SQLite using VB6

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 primary key, this automatically makes it an auto-increment column, and it works great.

I would like to insert a row into a table and get a new value with 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 get a newly created EventTypeID without having to issue another request (select max (EventTypeID) from EventType)?

It seems I remember from VB6 days a long time ago that there was a way to do this.

+9
sqlite vb6 auto-increment
Feb 10 '09 at 5:03
source share
5 answers

Does SQLite support SCOPE_IDENTITY?

Check out the FAQ . The sqlite3_last_insert_rowid () function will do this. Caution triggers though

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

 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 
+14
Feb 10 '09 at 5:15
source share

Run this query inside your code:

 SELECT SEQ from sqlite_sequence WHERE name='tablename' 
+3
Nov 19 '15 at 4:24
source share

I do not use VB, so I don’t know the interface that you use with your database, but as bendewey said, there is a function in c API with sqlite3_last_insert_rowid() that will return the primary integer key for the last inserted row so you can search a similar function in its interface.

If this does not work, you can use a SQLite-specific query:

 SELECT last_insert_rowid() 
+2
Feb 10 '09 at 5:20
source share

You can call SELECT last_insert_rowid() after inserting

+2
Feb 10 '09 at 5:21
source share

If used

 SELECT last_insert_rowid() 

returns the last rowid , you can use:

 SELECT last_insert_rowid() AS rowid FROM table_name LIMIT 1 

to get the last rowid for a single table

0
Mar 20 '14 at 8:15
source share



All Articles