read tables from SQL code

Discussion in 'Ruby' started by Bulhac Mihai, Oct 20, 2007.

  1. Bulhac Mihai

    Bulhac Mihai Guest

    hi,
    i have a SQL code like this one:

    SELECT Select_List
    FROM Table_List
    [WITH (BUFFERING = lExpr)]
    [WHERE Conditions]
    [GROUP BY Column_List]
    [UNION Clause]
    [HAVING Conditions]
    [ORDER BY Column_List]
    [INTO Clause | TO Clause ]
    [Additional_Display_Options]

    can i read/save with ruby the columns involved in that SQL select?
    --
    Posted via http://www.ruby-forum.com/.
     
    Bulhac Mihai, Oct 20, 2007
    #1
    1. Advertising

  2. > can i read/save with ruby the columns involved in that SQL select?

    If you use Sequel you can retrieve the columns for arbitrary SQL like
    this:

    require 'sequel/mysql' # assuming you're using MySQL
    DB = Sequel('mysql://localhost/mydb')
    DB['select * from items'].columns #=> [:id, :name, ...]

    But if you're already using Sequel why not construct your queries
    using Ruby instead of SQL, e.g.:

    dataset = DB.query do
    from :items
    where {:name =~ /^abc/ && :price < 100}
    order_by :name
    end
    p dataset.columns
    dataset.each {|r| p r}

    You can find more information about Sequel here:

    http://code.google.com/p/ruby-sequel

    And you can also get help on Sequel-talk:

    http://groups.google.com/group/sequel-talk

    best
    Sharon
     
    Sharon Rosner, Oct 20, 2007
    #2
    1. Advertising

  3. Bulhac Mihai

    John Joyce Guest

    Ruby DBI will let you do exact SQL, like DBI in other languages.

    ActiveRecord can do some of that select statement, but some of it no.
    ActiveRecord does have the ability to execute direct SQL statements
    though.
     
    John Joyce, Oct 20, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    3
    Views:
    4,379
    Bruce Barker
    Nov 4, 2005
  2. Peter Bassett
    Replies:
    3
    Views:
    933
    Augustus
    Aug 15, 2003
  3. Otuatail

    Tables within tables

    Otuatail, Jul 31, 2004, in forum: HTML
    Replies:
    7
    Views:
    499
  4. Chris Brat
    Replies:
    5
    Views:
    699
    =?iso-8859-1?q?Luis_M._Gonz=E1lez?=
    Aug 22, 2006
  5. Belinda
    Replies:
    4
    Views:
    368
    Bob Barrows [MVP]
    Jun 11, 2004
Loading...

Share This Page