简体   繁体   English

在 Rails 中,使用 mysql2 gem 从 MySQL 读取点类型时出现问题

[英]In Rails, problems reading a point type from MySQL with mysql2 gem

I'm working in a refactor from a legacy system and I'm having trouble with the "point" data type.我正在从旧系统进行重构,但在使用“点”数据类型时遇到了问题。

Inside Sequel Pro I can see the column "location" value as POINT(-22.81507676827597 -47.07767857976228)在 Sequel Pro 中,我可以将“位置”列的值视为POINT(-22.81507676827597 -47.07767857976228)

But when I query it from the database但是当我从数据库中查询时

Mysql2::Client.new(
          host: ENV.fetch('LEGACY_DATABASE_HOST'),
          username: ENV.fetch('LEGACY_DATABASE_USERNAME'),
          password: ENV.fetch('LEGACY_DATABASE_PASSWORD'),
          database: ENV.fetch('LEGACY_DATABASE_NAME')
        ).query("SELECT * FROM local").first('location')

I get a weird result like "\\x00\\x00\\x00\\x00\\x01\\x01\\x00\\x00\\x00\\x84y\\xFF\\xDE\\xA8\\xD06\\xC0\\xE0\\xD6'_\\xF1\\x89G\\xC0", if I try to save it right away, it becomes a 0.我得到一个奇怪的结果,如“\\x00\\x00\\x00\\x00\\x01\\x01\\x00\\x00\\x00\\x84y\\xFF\\xDE\\xA8\\xD06\\xC0\\xE0\\xD6'_\\xF1\\x89G\\xC0” ,如果我立即尝试保存它,它会变成 0。

How is the proper way to deal with point data type in mysql with rails?用rails处理mysql中的点数据类型的正确方法是什么?

I strongly recommend that you use the activerecord-postgis-adapter gem, makes all this very simple.我强烈建议您使用activerecord-postgis-adapter gem,使这一切变得非常简单。 Although if you're set on doing it yourself, dig into that gem and see how they do the point stuff.尽管如果您打算自己动手,请深入研究该宝石,看看他们是如何做到这point

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM