如何使用 VB6 在 SQLite 中返回 AUTO INCREMENT 列的值 [英] How to return the value of AUTO INCREMENT column in SQLite with VB6

查看:35
本文介绍了如何使用 VB6 在 SQLite 中返回 AUTO INCREMENT 列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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

这篇关于如何使用 VB6 在 SQLite 中返回 AUTO INCREMENT 列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆