Re: [sqlite] how to get last autoincrement value?

2005-08-03 Thread Dennis Cote

Austin Ziegler wrote:


On 8/3/05, Khamis Abuelkomboz <[EMAIL PROTECTED]> wrote:
 


Hi

I'm using SQLite in my application and am very excited about this little
and fast database.

How can I retrieve the last created autoincrement value?
On MySQL and SQLServer or just the SQL standard I can fire the following
select statement:
SELECT @@IDENTITY
   



Actually, as far as I know, SELECT @@IDENTITY isn't part of any ANSI
SQL standard. It cerrtainly won't work on Oracle ;)

That said, you can get the identity value of an autoincremented key
with sqlite3_last_insert_rowid
(http://sqlite.org/capi3ref.html#sqlite3_last_insert_rowid). It's
something of a cheat, but it works and works very well.

-austin
 

In SQLite you can also do the same thing directly from SQL if you want.  
There is a corresponding SQL function called last_insert_rowid(). So you 
can use;


select last_insert_rowid();

This will return the rowid of the last inserted row, which is the key 
for a table with an integer primary key (SQLite's version of 
autoincrement keys). It can also be used to get the rowid for tables 
with other kinds of keys or even no user specified key.


HTH
Dennis Cote





Re: [sqlite] how to get last autoincrement value?

2005-08-03 Thread Austin Ziegler
On 8/3/05, Khamis Abuelkomboz <[EMAIL PROTECTED]> wrote:
> Hi
> 
> I'm using SQLite in my application and am very excited about this little
> and fast database.
> 
> How can I retrieve the last created autoincrement value?
> On MySQL and SQLServer or just the SQL standard I can fire the following
> select statement:
> SELECT @@IDENTITY

Actually, as far as I know, SELECT @@IDENTITY isn't part of any ANSI
SQL standard. It cerrtainly won't work on Oracle ;)

That said, you can get the identity value of an autoincremented key
with sqlite3_last_insert_rowid
(http://sqlite.org/capi3ref.html#sqlite3_last_insert_rowid). It's
something of a cheat, but it works and works very well.

-austin
-- 
Austin Ziegler * [EMAIL PROTECTED]
   * Alternate: [EMAIL PROTECTED]