# \[ComboBox\] Populate from db table

**URL:** <https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639>\
**Category:** Ruby\
**Created:** [February 20, 2008, 8:59am UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639 "2008-02-20T08:59:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adaron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adaron](https://www.ruby-forum.com/u/adaron)\
**Post date:** [February 20, 2008, 8:59am UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639/1 "2008-02-20T08:59:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adaron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adaron](https://www.ruby-forum.com/u/adaron)\
**Post date:** [February 20, 2008, 11:47am UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639/2 "2008-02-20T11:47:38Z")

</div>

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

> **[The source of https://ruby-gnome.github.io/](https://ruby-gnome.github.io/)**
>
> The source of https://ruby-gnome.github.io/

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

HTH,

Howard

---

<div class="post-metadata">

**Author:** ![adaron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adaron](https://www.ruby-forum.com/u/adaron)\
**Post date:** [February 20, 2008, 11:53am UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639/3 "2008-02-20T11:53:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adaron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adaron](https://www.ruby-forum.com/u/adaron)\
**Post date:** [February 21, 2008, 11:54am UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639/4 "2008-02-21T11:54:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adaron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adaron](https://www.ruby-forum.com/u/adaron)\
**Post date:** [February 22, 2008, 10:00am UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639/5 "2008-02-22T10:00:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adaron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@adaron](https://www.ruby-forum.com/u/adaron)\
**Post date:** [February 20, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/combobox-populate-from-db-table/129639/6 "2008-02-20T12:11:09Z")

</div>

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](http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview)
