-2

I just installed the "Sequel" gem. I know that there are a lot of methods that make SQL easier, but I think that's just confusing.

Is there a way to send a raw SQL query so that:

table.all

would be?

"SELECT * FROM table"
the Tin Man
  • 150,910
  • 39
  • 198
  • 279
Sessho
  • 167
  • 7
  • 1
    It might seem confusing, but any new technology or language will seem confusing when you start. It's also really important to research the concept behind it, to read the documentation and determine whether the tool is of value to you, and whether you are willing to put in the effort to learn it. Well written ORMs, like Sequel, have many advantages over writing raw SQL and few disadvantages. You lose the advantages when you insist on writing the SQL yourself because you'll have to rewrite those queries if/when you change to a different DBM. Learn the ORM and that won't happen. – the Tin Man Apr 17 '17 at 22:09
  • 1
    Your question is covered in Sequel's documentation. The README and cheatsheet are excellent starting points to learn how it works. Bailing out and asking a question like this shows little effort. "[How much research effort is expected of Stack Overflow users?](http://meta.stackoverflow.com/questions/261592)" is relevant. – the Tin Man Apr 17 '17 at 22:11

2 Answers2

2

From documentation:

DB['SELECT * FROM table'].each do |row|
  p row
end
maicher
  • 2,426
  • 2
  • 13
  • 27
0

"You Can Just Use SQL" discusses how to embed SQL into Sequel expressions, but you're not going to take advantage of the power of an ORM if you do that. Instead, I'd recommend reading and playing with the examples in the "Cheat Sheet".

Here's a tweaked example from the README:

require 'sequel'

DB = Sequel.sqlite

DB.create_table :items do
  primary_key :id
  String :name
  Float :price
end

items = DB[:items]
items.insert(:name => 'abc', :price => rand * 100)
items.insert(:name => 'def', :price => rand * 100)
items.insert(:name => 'ghi', :price => rand * 100)

At this point, an in-memory SQLite DB would exist and would contain rows.

The base query of items is a select:

items.sql # => "SELECT * FROM `items`"

If we inspect the variable we get:

items # => #<Sequel::SQLite::Dataset: "SELECT * FROM `items`">

again showing the query.

The all method runs the query and returns the result:

items.all # => [{:id=>1, :name=>"abc", :price=>41.416988254871455}, {:id=>2, :name=>"def", :price=>40.44638070518766}, {:id=>3, :name=>"ghi", :price=>65.1898528586475}]

The same could be done using:

DB["select * from items"] # => #<Sequel::SQLite::Dataset: "select * from items">

and, again, all will retrieve the information from the DB and return it:

DB["select * from items"].all # => [{:id=>1, :name=>"abc", :price=>33.294219608356926}, {:id=>2, :name=>"def", :price=>29.25412438270755}, {:id=>3, :name=>"ghi", :price=>6.19681273957694}]

The doesn't begin to show the power of an ORM like Sequel though. Work through the README and then play with the examples in the Cheat Sheet and it'll make more sense.

the Tin Man
  • 150,910
  • 39
  • 198
  • 279