require 'mysql2'
@db.query("insert into clien开发者_如何学运维ts (Name) values ('#{client}')")
Can I return last inserted id with 1 query?
You can use last_id
method of the client instance:
client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "", :database => "db")
insert = client.query "INSERT INTO test (name, date) VALUES ('name', '2011-11-28 09:13:56')"
id = client.last_id
http://rubydoc.info/gems/mysql2/0.2.6/Mysql2/Client:last_id
Edit: it doesn't answer your original question but still looks better than call for LAST_INSERT_ID
I don't anything about gem but you can try to run
@db.query("SELECT LAST_INSERT_ID();")
after your first query.
精彩评论