We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
请更具体地说明您正在使用的 postgresql 库。
除了 ActiveRecord 之外,我将假设使用“pg”gem。
项目源有一个可能有用的 html 文件。
转到 https://bitbucket.org/ged/ruby-pg /src/b477174160c8/doc/postgres.html
然后点击html右上角的“raw”。在网络浏览器中打开该文件。
此示例代码可帮助您连接(从 html 文件复制):
res 对象是 PGResult。向下滚动到 html 中的该部分,查看可以调用哪些方法。
此链接有一个 PGResult 示例:
http://rubydoc.info/gems/pg/0.10.0/PGresult
摘抄:
Please be more specific about what postgresql library you're using.
I'm going to assume the 'pg' gem, apart from ActiveRecord.
The project source has an html file that might be helpful.
Go to https://bitbucket.org/ged/ruby-pg/src/b477174160c8/doc/postgres.html
Then click "raw" at the upper right side of the html. Open the file in your web browser.
This sample code helps you connect (copied from the html file):
The res object is a PGResult. Scroll down to that section in the html to see what methods you can call.
This link has a PGResult example:
http://rubydoc.info/gems/pg/0.10.0/PGresult
Excerpt:
我确认,“postgres”软件包已过时,您需要“pg”。
我花了很多时间才得到一个使用 ruby 和 postgres 的基本
select * from movie
。由于我很友善,这是我的代码:postgres Preparation (database=megatest user=roger pass=123456 table=films)
PG package Preparation
Ruby Code
Results
I confirm, "postgres" package is outdated, you need "pg".
It tooks me lot of time just to get a basic
select * from films
working with ruby and postgres. As I am kind, here is my code:postgres preparation (database=megatest user=roger pass=123456 table=films)
PG package preparation
Ruby Code
Results
您只需要需要
pg
gem 并建立与数据库的连接:当您定义从
ActiveRecord::Base
继承的模型时,它们将使用此数据库连接。其他一切都应该像 Rails 中一样工作。You only need to require the
pg
gem and establish the connection to the DB:When you define models to inherit from
ActiveRecord::Base
they will use this database connection. Everything else should work like it does in Rails.对于参数化 SQL 语句,您应该使用
PGconn#exec_params
,例如来源:http ://deveiate.org/code/pg/PGconn.html
在此处查看可传递给
PGconn
构造函数的参数的完整列表。For parametrized SQL statements, you should use
PGconn#exec_params
, e.g.Source: http://deveiate.org/code/pg/PGconn.html
Look here for a complete list of parameters that can be passed to the
PGconn
constructor.