就我的一生而言,我找不到一个简单的例子,仅仅运行这样的东西

"SELECT * FROM MyTable"

用Ruby 做的.我发现的所有东西都假设是ORM或Rails.现在,我不想要ORM;我不要铁轨.我正在寻找使用pg gem并执行简单查询的独立程序.

推荐答案

来自pg gem文档(http://rubydoc.info/gems/pg/0.10.0/frames)

require 'pg'
conn = PGconn.open(:dbname => 'test')
res  = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
res.getvalue(0,0) # '1'
res[0]['b']       # '2'
res[0]['c']       # nil

My next question would be authentication with a DB that requires a password. Looks like you can send a connection string like this:
PGconn.connect( "dbname=test password=mypass") or use the constuctor with parameters:
PGconn.new(host, port, options, tty, dbname, login, password) or use a hash like :password => '...' see here for all available options.

Ruby相关问答推荐

Ruby Case语句和固定,不适用于attr_reader

如何用 yield_self 断链

Ruby 中的 Hash[key] 返回 nil 但 hash 没有 nil 值

Ruby 和用正则表达式分割字符串

如何使用 define_method 指定方法默认参数?

当没有传入块时,是否有更好的方法来防止屈服?

在ruby中反转数组的顺序

用于 ruby​​ gems 的新 10.9 OSX 的命令行工具?

如何在 linux (ubuntu) 上更新 ruby​​?

在单个 node 上使用 XPath 返回所有 node 中的元素

Ruby字符串上的扫描和匹配有什么区别

理解 Ruby 中的私有方法

如何在没有 Rails 的情况下使用 RSpec?

确定一个值是否存在于哈希数组中

如何在Ruby中获取给定月份的天数,占年份?

Ruby - Array#<< 和 Array#push 之间的区别

如何在 jekyll markdown 博客中包含视频

将哈希传递给函数 ( *args ) 及其含义

如何使用 minitest 运行所有测试?

如何解决/usr/bin/env: ruby​​_executable_hooks: No such file or directory?