简体   繁体   English

使用Ruby和数据库[PostgreSQL]

[英]Working with Ruby and Databases [PostgreSQL]

I'm not versed working with DBs, but everytime I'm getting data from the DB with res = con.exec "SELECT * FROM #{table}" and then res.values I get all the values as if they were strings, for example: 我并不精通使用DB,但是每次我使用res = con.exec "SELECT * FROM #{table}"从数据库中获取数据,然后使用res.values我都会得到所有值,就像它们是字符串一样,例如:

[["1", "2017-01-14", "f"]]

Can I get Dates as Dates, integers as integers, etc.? 我可以将日期作为日期,将整数作为整数等吗? or do I have to parse everything? 还是我必须解析所有内容?

EDIT: I'm using Ruby without Rails. 编辑:我正在使用没有Rails的Ruby。

The documentation for the PG gem details how to handle basic typecasting: PG gem文档详细说明了如何处理基本类型转换:

conn.type_map_for_results = PG::BasicTypeMapForResults.new conn
conn.exec("select 1, now(), '{2,3}'::int[]").values
    # => [[1, 2014-09-21 20:51:56 +0200, [2, 3]]]

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

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