[ComboBox] Populate from db table

M

Mattia Galati

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.
 
H

Howard Roberts

Mattia said:
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
 
M

Mattia Galati

Howard said:
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
 
H

Howard Roberts

Mattia said:
/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
 
M

Mattia Galati

[...] 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
 
T

Thufir

@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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top