Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recommend avoiding writing raw SQL, and, in this age, I'd use an ORM, even for simple DB use. I highly recommend using the <a href="http://sequel.rubyforge.org/documentation.html">Sequel</a> gem. Borrowing from an <a href="http://sequel.rubyforge.org/rdoc/files/README_rdoc.html">example</a> in the Sequel docs:</p> <pre><code>sequel sqlite://temp Your database is stored in DB... </code></pre> <p>Using SQLite, that started the sequel ORM in interactive mode, and created the SQLite database called "temp".</p> <pre><code>ruby-1.9.2-p290 :001 &gt; require 'logger' =&gt; true ruby-1.9.2-p290 :002 &gt; DB.loggers &lt;&lt; Logger.new(STDOUT) =&gt; [#&lt;Logger:0x0000010160bc40 @progname=nil, @level=0, @default_formatter=#&lt;Logger::Formatter:0x0000010160bc18 @datetime_format=nil&gt;, @formatter=nil, @logdev=#&lt;Logger::LogDevice:0x0000010160bbc8 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#&lt;IO:&lt;STDOUT&gt;&gt;, @mutex=#&lt;Logger::LogDevice::LogDeviceMutex:0x0000010160bba0 @mon_owner=nil, @mon_count=0, @mon_mutex=#&lt;Mutex:0x0000010160bb50&gt;&gt;&gt;&gt;] </code></pre> <p>That enabled logging so we can see what Sequel will do as it talks to the database.</p> <pre><code>ruby-1.9.2-p290 :003 &gt; items = DB[:items] # Create a dataset =&gt; #&lt;Sequel::SQLite::Dataset: "SELECT * FROM `items`"&gt; ruby-1.9.2-p290 :004 &gt; DB.tables I, [2011-11-25T10:17:13.056311 #10130] INFO -- : (0.000501s) SELECT * FROM `sqlite_master` WHERE (type = 'table' AND NOT name = 'sqlite_sequence') =&gt; [] </code></pre> <p>Doh! I forgot to create the table...</p> <pre><code>ruby-1.9.2-p290 :005 &gt; DB.create_table :items do ruby-1.9.2-p290 :006 &gt; primary_key :id ruby-1.9.2-p290 :007?&gt; String :name ruby-1.9.2-p290 :008?&gt; Float :price ruby-1.9.2-p290 :009?&gt; end I, [2011-11-25T10:17:20.985851 #10130] INFO -- : (0.002372s) CREATE TABLE `items` (`id` integer PRIMARY KEY AUTOINCREMENT, `name` varchar(255), `price` double precision) =&gt; nil </code></pre> <p>The table is created.</p> <pre><code>ruby-1.9.2-p290 :010 &gt; items = DB[:items] # Create a dataset =&gt; #&lt;Sequel::SQLite::Dataset: "SELECT * FROM `items`"&gt; </code></pre> <p>That created a dataset, which is just a convenient way to talk to a table.</p> <p>And, that's the payoff:</p> <pre><code>ruby-1.9.2-p290 :011 &gt; items.insert(:name =&gt; "Joan d'Arc") I, [2011-11-25T10:17:45.186945 #10130] INFO -- : (0.001981s) INSERT INTO `items` (`name`) VALUES ('Joan d''Arc') =&gt; 1 </code></pre> <p>The ORM automatically escapes the characters for you. Your job is simplified greatly.</p> <p>ORMs are DBM aware, so they know when to escape for a particular database. Your code doesn't change. It's trivial to switch from SQLite to MySQL, Postgres, or even non-SQL databases.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload