使用 Sinatra 时与数据库对话的最佳方式是什么? [英] What's the best way to talk to a database while using Sinatra?

查看:15
本文介绍了使用 Sinatra 时与数据库对话的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Sinatra 框架与 Rails 不同,不提供 ORM.在这种情况下,您如何与 Sinatra 应用程序中的数据库对话?还是 Sinatra 仅适用于不使用数据库的应用程序?

As I understand it, the Sinatra framework, unlike Rails, does not provide an ORM. In that case, how do you talk to a DB in a Sinatra app? Or is Sinatra only for apps that don't use a DB?

推荐答案

如果您喜欢 ActiveRecord,请使用它.或者是其他东西.例如,数据映射器.对于带有 SQLite 的 AR,这是有效的:

If you like ActiveRecord, use that. Or something else. Datamapper, for instance. For AR with SQLite, this works:

require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'active_record'

class Article < ActiveRecord::Base
end

get '/' do
  Article.establish_connection(
    :adapter => "sqlite3",
    :database => "hw.db"
  )
  Article.first.title
end

这篇关于使用 Sinatra 时与数据库对话的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆