Mysql/Ruby Sequel最后一次插入ID值,用什么方法?

标签 mysql ruby sequel

我只想使用 Ruby 的 Sequel 获取 last_insert_id():

insertret = @con.run("INSERT INTO `wv_persons` ( `id` ) VALUES ( NULL )")
pp insertret.inspect # returns "nil", expected that..
last_insert_id = @con.run("SELECT LAST_INSERT_ID() AS last_id;")
pp last_insert_id.inspect # returns "nil", should be an ID

SELECT 查询应该返回 last_id 但 .run 不返回它。我应该改用什么方法?

解决方案:(感谢 Josh Lindsey)

last_insert_id = @con[:wv_persons].insert({})
last_insert_id = last_insert_id.to_s
puts "New person ["+ last_insert_id  +"]"

最佳答案

Dataset#insert方法应该返回最后插入的 id:

DB[:wv_persons].insert({})

将插入默认值并返回 id。

Database#run将始终返回 nil

关于Mysql/Ruby Sequel最后一次插入ID值,用什么方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3478024/

相关文章:

php - 用 php 在 mysql 中操作 15+ 百万条记录?

mysql - DATE_SUB(NOW(), INTERVAL 14 DAY) 不工作

ruby-on-rails - LESS CSS 预处理器 : Is there a way to map a single color to an rgb and rgba definition?

ruby - 如何使用续集创建数据库

ruby - Sequel::Model:像create_table这样的方法从哪里来?

ruby - 未找到续集模型 set_schema

php - 选择使用双引号字符串时,mysql 上出现错误 1064 <42000>

mysql - SQL UPDATE 或 DELETE 如果重复

Ruby - 如何向散列添加条件键?

ruby - proc 和 lambda 之间有什么区别?