Forum: Ruby In doubt with sqlite3-ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
aurelianito (Guest)
on 2006-02-26 17:49
(Received via mailing list)
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.
E. Saynatkari (Guest)
on 2006-02-26 18:13
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
aurelianito (Guest)
on 2006-02-26 20:09
(Received via mailing list)
> > 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.
This topic is locked and can not be replied to.