12
我有這樣的代碼,最後插入的ID?獲取使用mysql2寶石
我有這樣的代碼,最後插入的ID?獲取使用mysql2寶石
您可以使用客戶端實例的last_id
方法:
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
我不是寶石什麼,但你可以試試你的第一次查詢後運行
@db.query("SELECT LAST_INSERT_ID();")
。
日Thnx更好,我知道這件事。 在postgres中,我可以在1個關鍵字RETURNING ID的查詢中做到這一點,我想在這裏使用像這樣的事情 – babi4