[ComboBox] Populate from db table

Discussion in 'Ruby' started by Mattia Galati, Feb 20, 2008.

  1. Hy everybody, I've just started learning a bit of this amazing language
    but I've encountered some problems.
    I have a sqlite3 database wich contains a table and I want to populate a
    Gtk::ComboBox with the data on it.
    To create the GUI I've used Glade and this is a part of my code:

    @db.execute("SELECT cat FROM table ORDER BY cat") do |row|
    puts row
    end

    Now, the script print the rows correctly, but I can't manage to store
    them in my ComboBox called trough

    @combo = @glade["my_combo"]

    what should I do?

    Sorry for my bad english.
    --
    Posted via http://www.ruby-forum.com/.
     
    Mattia Galati, Feb 20, 2008
    #1
    1. Advertising

  2. Mattia Galati wrote:

    > I have a sqlite3 database wich contains a table and I want to populate a
    > Gtk::ComboBox with the data on it.


    Hi,

    if you have:

    rows= @db.execute("SELECT cat FROM table ORDER BY cat")

    that should return an array of rows from your table. Each of these rows
    themselves be an array of the fields from that row. So you should be
    able to do something like this:

    rows.each{|r| @combo.append_text(r[0])}

    For documentation on RubyGTK2 you may want to have a look at
    http://ruby-gnome2.sourceforge.jp/

    There is also a list for RubyGTK2 here
    http://www.ruby-forum.com/forum/12

    HTH,

    Howard

    --
    Posted via http://www.ruby-forum.com/.
     
    Howard Roberts, Feb 20, 2008
    #2
    1. Advertising

  3. Howard Roberts wrote:
    > So you should be
    > able to do something like this:
    >
    > rows.each{|r| @combo.append_text(r[0])}
    >

    for each row in the table this code give me this error:

    /usr/lib/ruby/1.8/sqlite3/database.rb: line 643
    Gtk-CRITICAL **:gtk_combo_box_append_text: assertion
    `GTK_IS_LIST_STORE (combo_box->priv->model)' failed

    note that the combo was not created with Gtk::ComboBox.new but retrieved
    with @glade["combo"] ... don't know if it matters
    --
    Posted via http://www.ruby-forum.com/.
     
    Mattia Galati, Feb 20, 2008
    #3
  4. Mattia Galati wrote:

    > /usr/lib/ruby/1.8/sqlite3/database.rb: line 643
    > Gtk-CRITICAL **:gtk_combo_box_append_text: assertion
    > `GTK_IS_LIST_STORE (combo_box->priv->model)' failed
    >
    > note that the combo was not created with Gtk::ComboBox.new but retrieved
    > with @glade["combo"] ... don't know if it matters


    Hmm...The ComboBox documentation would lead me to believe that
    append_text() should work for your needs if you were writing your
    script from scratch, but it looks like Glade is forcing your hand into
    using the more robust(complex?) MVC based TreeModel structure, for which
    there is a bit of a learning curve :)

    See this for the details:

    http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview



    --
    Posted via http://www.ruby-forum.com/.
     
    Howard Roberts, Feb 20, 2008
    #4
  5. > [...] it looks like Glade is forcing your hand into
    > using the more robust(complex?) MVC based TreeModel structure, for which
    > there is a bit of a learning curve :)


    yes, you're right. However I managed to run, here the code:


    combo_cat = @glade["combo_categoria"]
    list_cat = Gtk::ListStore.new(String, String)
    cell_cat = Gtk::CellRendererText.new()
    combo_cat.pack_start(cell_cat, true)
    combo_cat.add_attribute(cell_cat, 'text',0)
    combo_cat.set_model(list_cat)
    @db.execute("SELECT categoria FROM categorie_clienti ORDER BY
    categoria") do |row|
    iter = list_cat.append
    iter[0] = row[0]
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Mattia Galati, Feb 21, 2008
    #5
  6. Mattia Galati

    Thufir Guest

    On Wed, 20 Feb 2008 16:59:36 +0900, Mattia Galati wrote:

    > @db.execute("SELECT cat FROM table ORDER BY cat") do |row|
    > puts row
    > end
    >
    > Now, the script print the rows correctly, but I can't manage to store
    > them in my ComboBox called trough
    >
    > @combo = @glade["my_combo"]



    I like the way you think about ruby and SQL. May I ask if you got this
    from a book?


    thanks,

    Thufir
     
    Thufir, Feb 22, 2008
    #6
    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. Kent Johnson

    Populate from a Table..?

    Kent Johnson, Nov 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    352
    Sergey Poberezovskiy
    Nov 24, 2003
  2. Nithin
    Replies:
    0
    Views:
    623
    Nithin
    Dec 31, 2003
  3. Enjoy Life
    Replies:
    2
    Views:
    314
    Roland Hall
    Feb 23, 2005
  4. Replies:
    0
    Views:
    480
  5. Replies:
    5
    Views:
    505
    Roedy Green
    Oct 11, 2012
Loading...

Share This Page