[ComboBox] Populate from db table

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.

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

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

HTH,

Howard

Howard R. 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

[…] 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 :slight_smile:

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

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

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 :slight_smile:

See this for the details:

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