使用mysql2 gem获取最后插入的id [英] Get last inserted id using mysql2 gem
本文介绍了使用mysql2 gem获取最后插入的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
I have code like this:
require 'mysql2'
@db.query("insert into clients (Name) values ('#{client}')")
我可以返回带有1个查询的最后插入的ID吗?
Can I return last inserted id with 1 query?
推荐答案
您可以使用客户端实例的last_id
方法:
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
编辑:它不能回答您原来的问题,但看起来比要求LAST_INSERT_ID更好
Edit: it doesn't answer your original question but still looks better than call for LAST_INSERT_ID
这篇关于使用mysql2 gem获取最后插入的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文