如何使用 VB6 在 SQLite 中返回 AUTO INCREMENT 列的值

2021-11-30 00:00:00 sqlite auto-increment vb6

我在 SQLite 中有一个表:

创建表事件类型"([EventTypeID] 整数主键,[EventTypeName] VARCHAR(50) NOT NULL UNIQUE);

由于 EventTypeID 是一个整数和一个主键,这会自动使它成为一个自动递增的列,并且工作正常.

我想在表中插入一行并从 VB6 中获取新增加的值.

Dim oRs as Recordset将 oCmd 调为新命令oCmd.ActiveConnection = GetConnection()oCmd.Source = "插入 EventType (EventTypeName) 值 ('blah')"oCmd.执行

是否有一种自动检索新创建的 EventTypeID 而无需发出另一个查询的方法(从 EventType 中选择 max(EventTypeID))?

我似乎记得很久以前的 VB6 天,有一种方法可以做到这一点.

解决方案

SQLite 是否支持 SCOPE_IDENTITY?

一个>

<块引用>

查看常见问题解答.这sqlite3_last_insert_rowid()函数会做到的.小心的虽然触发

未经测试,但您应该能够在一次调用中发送两个语句.自从我写任何 VB6 以来已经有一段时间了.这也不是 SQL 注入安全.

Dim oRs as Recordset将 sSql 调暗为字符串sSql = "INSERT INTO EventType (EventTypeName) VALUES ('blah'); SELECT last_insert_rowid() FROM EventType"oRs.Open sSql oConn

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.

解决方案

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

相关文章