在使用Sinatra时,与数据库交谈的最好方法是什么? [英] What's the best way to talk to a database while using Sinatra?

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

问题描述

根据我的理解,Sinatra框架,不像Rails,不提供ORM。在这种情况下,您如何在Sinatra应用程序中与数据库沟通?或者是Sinatra只适用于不使用DB的应用程序?

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,或者是其他东西。例如,Datamapper。对于AR与SQLite,这工作:

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 'activerecord'

class Article < ActiveRecord::Base
end

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

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

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