1

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:

[["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.

4
  • Are you using Rails, or a database adapter like pg directly? Commented Jan 14, 2017 at 16:53
  • No rails, pg directly with ruby. Commented Jan 14, 2017 at 16:56
  • what database do you use, what type has your date in your database ? date or string ? Commented Jan 14, 2017 at 16:58
  • In the example that I shown it was INTEGER, DATE, BOOLEAN, but everything shows as string in Ruby and I use postgres. Commented Jan 14, 2017 at 16:59

1 Answer 1

3

The documentation for the PG gem details how to handle basic typecasting:

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]]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.