如何从 SQLite 表中检索最后一个自动递增的 ID?

2023-07-17数据库问题
2

本文介绍了如何从 SQLite 表中检索最后一个自动递增的 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个表消息,其中包含列 ID(主键、自动增量)和内容(文本).
我有一个表用户,其中包含用户名(主键、文本)和哈希列.
一条消息由一个发送者(用户)发送给多个接收者(用户),一个接收者(用户)可以有很多条消息.
我创建了一个包含两列的 Messages_Recipients 表:MessageID(指的是 Messages 表的 ID 列和 Recipient(指的是 Users 表中的 username 列).该表表示收件人和消息之间的多对多关系.

所以,我的问题是这个.新消息的 ID 将在它存储在数据库中后创建.但是我如何保存对刚刚添加的 MessageRow 的引用以检索这个新的 MessageID?
当然,我总是可以在数据库中搜索添加的最后一行,但这可能会在多线程环境中返回不同的行吗?

I have a table Messages with columns ID (primary key, autoincrement) and Content (text).
I have a table Users with columns username (primary key, text) and Hash.
A message is sent by one Sender (user) to many recipients (user) and a recipient (user) can have many messages.
I created a table Messages_Recipients with two columns: MessageID (referring to the ID column of the Messages table and Recipient (referring to the username column in the Users table). This table represents the many to many relation between recipients and messages.

So, the question I have is this. The ID of a new message will be created after it has been stored in the database. But how can I hold a reference to the MessageRow I just added in order to retrieve this new MessageID?
I can always search the database for the last row added of course, but that could possibly return a different row in a multithreaded environment?

据我了解,对于 SQLite,您可以使用 SELECT last_insert_rowid().但是我如何从 ADO.Net 调用这个语句?

As I understand it for SQLite you can use the SELECT last_insert_rowid(). But how do I call this statement from ADO.Net?

我的持久化代码(消息和消息接收者是数据表):

My Persistence code (messages and messagesRecipients are DataTables):

public void Persist(Message message)
{
    pm_databaseDataSet.MessagesRow messagerow;
    messagerow=messages.AddMessagesRow(message.Sender,
                            message.TimeSent.ToFileTime(),
                            message.Content,
                            message.TimeCreated.ToFileTime());
    UpdateMessages();
    var x = messagerow;//I hoped the messagerow would hold a
    //reference to the new row in the Messages table, but it does not.
    foreach (var recipient in message.Recipients)
    {
        var row = messagesRecipients.NewMessages_RecipientsRow();
        row.Recipient = recipient;
        //row.MessageID= How do I find this??
        messagesRecipients.AddMessages_RecipientsRow(row);
        UpdateMessagesRecipients();//method not shown
    } 

}

private void UpdateMessages()
{
    messagesAdapter.Update(messages);
    messagesAdapter.Fill(messages);
}

推荐答案

使用 SQL Server,您将 SELECT SCOPE_IDENTITY() 获取当前进程的最后一个标识值.

With SQL Server you'd SELECT SCOPE_IDENTITY() to get the last identity value for the current process.

使用 SQlite,它看起来就像你会做的自动增量

With SQlite, it looks like for an autoincrement you would do

SELECT last_insert_rowid()

在您插入后立即.

http://www.mail-archive.com/sqlite-users@sqlite.org/msg09429.html

在回答您的评论以获得此值时,您需要使用 SQL 或 OleDb 代码,例如:

In answer to your comment to get this value you would want to use SQL or OleDb code like:

using (SqlConnection conn = new SqlConnection(connString))
{
    string sql = "SELECT last_insert_rowid()";
    SqlCommand cmd = new SqlCommand(sql, conn);
    conn.Open();
    int lastID = (Int32) cmd.ExecuteScalar();
}

这篇关于如何从 SQLite 表中检索最后一个自动递增的 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

定义外键有什么好处
what are the advantages of defining a foreign key(定义外键有什么好处)...
2024-04-16 数据库问题
7

音乐库 MySQL 数据库
Music library MySQL database(音乐库 MySQL 数据库)...
2024-04-16 数据库问题
6

理解mysql解释
understanding mysql explain(理解mysql解释)...
2024-04-16 数据库问题
8

价格是浮点数还是小数点?
Float or decimal for prices?(价格是浮点数还是小数点?)...
2024-04-16 数据库问题
5