When I comprehend it, the Sinatra framework, unlike Rails, doesn't offer an ORM. For the reason that situation, how can you speak with a DB inside a Sinatra application? Or perhaps is Sinatra just for applications that do not make use of a DB?
If you are using Sinatra, I can not recommend DataMapper highly enough. I've got a couple Rails applications where I am tied to ActiveRecord, and I am constantly cursing its weak points and design defects. If you are on Sinatra, DataMapper is an extremely viable option.
require "rubygems" require "sinatra" require "datamapper" DataMapper.setup(:default, "sqlite3::memory:") class Post include DataMapper::Resource property :id, Integer, :serial => true property :title, String end Post.auto_migrate! first_post = Post.new first_post.title = "First!" first_post.save get "/" do Post.get(1).title end
If you want ActiveRecord, use that. Or something like that else. Datamapper, for example. 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