alex brie . net

bloghez din 2003 despre mine, internet, prieteni si altele.
  • Blog
  • About
  • Contact
  • Articole preferate
  • Publicitate
  • Blogs
« Salvati un presedinte!
V1 »

Setting up Rails and SQLITE

April 25th, 2007 |

If you are interested in web, databases and the like then you probably already heard of SQLITE. If not, here’s a quick intro
SQLITE is a database engine that uses just one file per database. It’s tiny, simple to use and, most importantly, very easy to use with Ruby on Rails. All you need is the sqlite ruby gem, which is already included in most Ruby installations.

What is sqlite3? Nothing much than a newer and better version of sqlite. You should use it instead of sqlite if you have the chance.

How do you set it up in Rails?
Let’s say you want to make your own database-driven Rails application. Mysql is a big download, a bit difficult to install, etc. Plus, you don’t really need it; you just want a tiny sql database engine that works with Rails, so you can develop and test your application.
Nothing easier: if not already installed on your machine, then you download and install sqlite3.
Then you make sure you have the latest sqlite bindings in place : gem install sqlite3 .

Hmm.. what else?

oh… the config/database.yml file.

development:
    adapter: sqlite3
    database: db/development.sqlite3

test:
    adapter: sqlite3
    database: db/test.sqlite3

production:
    development

Well now; you are set up. Just go ahead, create your database structure. How? Edit db/schema.rb and start adding tables definitions in a ruby syntax; something like this:


ActiveRecord::Schema.define do

  create_table "users", :force => true do |t|
    t.column :login, :string, :limit => 80
    t.column :cryptpassword, :string, :limit => 40
    t.column :email, :string, :limit => 100
    t.column :created_at, :datetime
    t.column :updated_at, :datetime
  end

  create_table :article, :force => true  do |t|
      t.column :title, :string
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
      t.column :content, :text
  end
end

Warning! Back up this file also to schema_backup.rb

To import this schema in your database, all you have to do is run rake db_schema_import

You see? All set now. You can start generating your scaffolding and code away.

How to take your data OUT of sqlite?
If at some point in the future, you want to get your data OUT of the sqlite3 database, all you have to do is a sqlite3 dump:
run

sqlite3 db/development.sqlite3
.output my_sql_dump.sql
.dump
.exit

PS. I learned about the commands to use by peeking at sqlite3’s help over here
You’ll have to remove some of the lines at the beginning and the end of the generated my_sql_dump.sql file. But the result is a SQL file that can be later imported into MYSQL or other SQL database engine, via classic import commands.

Wel… wasn’t this easy? I sure did learn a lot. :) [tags]rails, ruby, sqlite, sqlite3, rake[/tags]

Daca esti nou pe aici, nu uita sa te abonezi la feedul meu RSS . Iti multumesc pentru vizita!


Postata in Rails, Ruby, Tutorials | RSS 2.0 feed comentarii | Comenteaza; Trackback.

2 Responses to “Setting up Rails and SQLITE”

  1. Gravatar David:
    May 1st, 2007 at 12:48 am

    Very helpful article, thanks

  2. Gravatar Simeon:
    June 6th, 2007 at 12:54 am

    Thanks for your article. I just tried to install sqlite3 and ended up needing to run this instead

    gem install sqlite3-ruby

    But otherwise the instructions were great!

Leave a Reply

  • Despre Alex Brie

    View Alexandru Brie's profile on LinkedIn

    Bloger pasionat din septembrie 2003.
    Software engineer; master in cercetare algoritmica la Ecole Polytechnique Paris; a lucrat in Paris in software bursier.

    Inapoi in Romania din 2006, programeaza Ruby on Rails pentru un startup american, este consultant independent, blogger si antreprenor cu diverse proiecte online
    [afla mai multe..]

    Aboneaza-te RSS(Romanian)

      
    Acum te poti abona si prin email.

    Subscribe(English)

    Twitter


  • Recomand

    jurnal de fapte bune eLiberatica 2008 - The Benefits of Open and Free Technologies Conference cele mai importante bloguri din .ro, pe o singura pagina
  • Articole recente

    • La Radio.. vreau sa ajung la Radio…
    • Hotii de cuvinte
    • BD cu Emo
    • 4 ponturi despre cum s-ar putea imbunatati Vodafone *222 si nu numai
    • Oscarul zilei la categoria bloguri
    • Publicitati pe bloguri si testari de produse
    • Scurte ale ultimelor zile
    • Videoclipuri facute de fani.. amatori?
    • De ce a devenit stupid sa-i injuri pe bloggeri.
    • Marius Tuca - onanist ca toti onanistii…
    • Vodafone steluta 222 si un glont in cap
    • Matrix pi dulsili grai basarabean
    • Bobby Yahoo
    • Blog Marketing Survey
    • Cauta pe Trilulilu direct din Firefox
  • Comentarii recente

    • A.Faith on La Radio.. vreau sa ajung la Radio…
    • Handy on Functional tests with login in Rails
    • sorecarul on Hotii de cuvinte
    • Janos on BD cu Emo
    • Ahilosu on BD cu Emo
    • Anal Avenger on The most amazing useless talent I ever saw
    • Kappacelu` on Hotii de cuvinte
    • Piku on Banca Transilvania
    • Alina on BD cu Emo
    • A.Faith on Videoclipuri facute de fani.. amatori?
    • A.Faith on Publicitati pe bloguri si testari de produse
    • bubu on BD cu Emo
    • alexandru petria on BD cu Emo
    • 4 ponturi despre cum s-ar putea imbunatati Vodafone *222 si nu numai | alex brie . net on Vodafone steluta 222 si un glont in cap
    • Alex on Continuare la C-net/Ilink - din ce in ce mai varza…
  • Archives

  • Categories

    • Blog (1268)
      • carcoteli (3)
      • English (12)
      • evenimente (3)
      • FILMS (33)
      • French (3)
      • fun (54)
      • intrebari (4)
      • jocuri (3)
      • matematica (1)
      • PHOTO (143)
        • desene (11)
        • flori (2)
        • oua rosii (1)
      • Projects (75)
        • business (29)
        • ROSI (11)
      • Romanian (114)
        • aici (46)
        • repat (33)
      • scurte (7)
      • Software (23)
        • .mac (20)
      • stiati ca (4)
      • Stiri IT Media si Internet (38)
      • Technicalities (65)
      • Web (447)
        • links (147)
        • Memes (54)
        • video (52)
    • dev (19)
      • Open Source (1)
      • Rails (7)
      • Ruby (7)
      • Textpattern (2)
      • Tips and Tricks (8)
      • Tutorials (6)
      • Wordpress (6)
    • Words (127)
      • essays (31)
      • Politice (38)
      • vss[very short stories] (5)
  • Poze recente

    IMG_0319 IMG_0286 IMG_0284 IMG_0334 IMG_0279 IMG_0282 IMG_0271 IMG_0323 IMG_0278 IMG_0336 IMG_0281 IMG_0269 IMG_0315 IMG_0289 IMG_0326 IMG_0316 IMG_0324 IMG_0349 IMG_0351 IMG_0331
    View more photos >
  • Tags

    Apple Blog blogging blogosfera bloguri blogvertising bucuresti business comics Digg FILMS fun Google internet intrebari iPhone iread jocuri Leopard links Manafu marketing myblog online OSX personal PHOTO Politice poze proiecte publicitate Rails roblogfest Romania Romanian Ruby spam stiri twitter viata video viral Web Wordpress youtube

Entries (RSS) and Comments (RSS).
Theme inspired by CrunchNotes, by Alex Brie.

Copyright © 2005-2008 alex brie . net. Unele articole pot contine tematici sau linkuri sponsorizate. Opiniile si idele sunt intodeauna ale autorului, care le imbratiseaza si le sustine.