# In doubt with sqlite3-ruby

**URL:** <https://www.ruby-forum.com/t/in-doubt-with-sqlite3-ruby/73477>\
**Category:** Ruby\
**Created:** [February 26, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/in-doubt-with-sqlite3-ruby/73477 "2006-02-26T16:49:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aurelianito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@aurelianito](https://www.ruby-forum.com/u/aurelianito)\
**Post date:** [February 26, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/in-doubt-with-sqlite3-ruby/73477/1 "2006-02-26T16:49:51Z")

</div>

Hi!  
I have a table with an AUTOINCREMENT primary key and I would like to  
know how to retrieve this primary key when a new row is inserted. Is it  
possible?

Below, I written some sample ruby code for you to see my problem.

require ‘sqlite3’  
db = SQLite3::Database.new( “test.db” )  
db.execute “create table if not exists t1(id integer PRIMARY KEY  
AUTOINCREMENT, v1, v2);”

# some code, skipped, may include inserts to t1

db.execute “insert into t1 (v1,v2) values (1,2)” # don’t know the  
primary key assigned.  
db.execute “insert into t1 (v1,v2) values (3,4)” # don’t know the  
primary key assigned.

Thank’s in advance,  
Aureliano.

---

<div class="post-metadata">

**Author:** ![aurelianito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@aurelianito](https://www.ruby-forum.com/u/aurelianito)\
**Post date:** [February 26, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/in-doubt-with-sqlite3-ruby/73477/2 "2006-02-26T17:13:48Z")

</div>

aurelianito wrote:

> Hi!  
> I have a table with an AUTOINCREMENT primary key and I would like to  
> know how to retrieve this primary key when a new row is inserted. Is it  
> possible?
> 
> Below, I written some sample ruby code for you to see my problem.
> 
> require ‘sqlite3’  
> db = SQLite3::Database.new( “test.db” )  
> db.execute “create table if not exists t1(id integer PRIMARY KEY  
> AUTOINCREMENT, v1, v2);”

Just INTEGER PRIMARY KEY should be enough.

> # some code, skipped, may include inserts to t1
> 
> db.execute “insert into t1 (v1,v2) values (1,2)” # don’t know the  
> primary key assigned.  
> db.execute “insert into t1 (v1,v2) values (3,4)” # don’t know the  
> primary key assigned.

The method is called last\_insert\_rowid, I think,  
check the API docs to verify.

> Thank’s in advance,  
> Aureliano.

E

---

<div class="post-metadata">

**Author:** ![aurelianito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@aurelianito](https://www.ruby-forum.com/u/aurelianito)\
**Post date:** [February 26, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/in-doubt-with-sqlite3-ruby/73477/3 "2006-02-26T19:09:47Z")

</div>

> > AUTOINCREMENT, v1, v2);"  
> > The method is called last\_insert\_rowid, I think,  
> > check the API docs to verify.
> 
> > Thank’s in advance,  
> > Aureliano.

The method is last\_insert\_row\_id (note the “\_” between row and id).  
Thank you very much for the tip (I didn’t knew where look for it).

Aureliano.
