sqlite Last_Insert_Rowid()始终返回0 [英] sqlite Last_Insert_Rowid() returns 0 always

查看:684
本文介绍了sqlite Last_Insert_Rowid()始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

我遇到sqlite问题

Hi..
Im having a problem with sqlite

Last_Insert_Rowid()



函数..



这是我试过的代码..




function..

Here's the code Ive tried..

SQLiteConnection con = new SQLiteConnection(conString);
            con.Open();

            string cmdtxt = @"select Last_Insert_Rowid() from Customers";
//            string cmdtxt = @"insert into Customers(Name, TP, Address, Email, Notes, ModifiedTime)
//                                values ('asdasd', 'dgdfg', 'dfg', 'dfg', 'dfg', 'dfg')";


            SQLiteCommand cmd = con.CreateCommand();
            cmd.CommandText = cmdtxt;

            object obj = cmd.ExecuteScalar();
            Console.WriteLine(obj.ToString());





当我运行它时,它总是在cmd中显示0 ..

但是当我在sqliteAdmin IDE中运行相同的查询时它会显示23 ..

我的代码有什么问题?



When I run this, it always display the 0 in cmd..
but when I run the same query in the sqliteAdmin IDE it displays the 23..
What's the wrong with my code ?

推荐答案

作为 SQLite文档 [ ^ ]说:



As the SQLite documentation[^] says:

The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function. The last_insert_rowid() SQL function is a wrapper around the sqlite3_last_insert_rowid() C/C++ interface function.



这实际上意味着它只能在同一个连接中工作(而且我'只能在同一个命令中使用它。)

一旦你打开一个新的连接,你总是得到0,直到你在该连接上执行插入。

我?我这样做:


Which means in essence that it only works within the same Connection (And I'd only use it inside the same Command).
Once you open a new connection, you will always get 0 until you perform an insert on that connection.
Me? I'd do this:

INSERT INTO MyTable (Column1) VALUES(6); SELECT Last_Insert_Rowid();

作为我的SQLiteCommand ...

as my SQLiteCommand...


这篇关于sqlite Last_Insert_Rowid()始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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