[ruby-gnome2-doc-cvs] [Hiki] create - tut-libgda-datamodels

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2003年 9月 22日 (月) 06:44:18 JST


-------------------------
REMOTE_ADDR = 217.117.55.140
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/?tut-libgda-datamodels
-------------------------
= Managing data models

Each time we execute a normal query, we will obtain a Gda::DataModel object, which is the way to see what the query returned.

Before continuing, we must say that it is possible to modify a data model, but as we are accessing using SQL, it is not recommended to modify it, so modifications on the database must be done using SQL.

== Example using direct cell access

This method accesses the data model by directly accessing cells, using Gda::DataModel#get_value_at:

  def show_table(dm)
      # Loop for writing column names.
      dm.n_columns.times { |n_col| puts dm.get_column_title(n_col) }
      puts ""
      # Double loop accessing values using Gda::DataModel#get_value_at.
      dm.n_columns.times do |n_col|
          dm.n_rows.times do |n_row|
              # Data returned is a Gda::Value object.
              val = dm.get_value_at(n_col, n_row)
              puts val.to_s
          end
      end
  end

== Example using row access

This method accesses the data model by accessing rows, using Gda::DataModel#get_row and Gda::DataModel#get_value:

  def show_table2(dm)
      # Loop for writing column names.
      dm.n_columns.times { |n_col| puts dm.get_column_title(n_col) }
      puts ""
      # Outer loop obtaining rows using Gda::DataModel#get_row.
      dm.n_rows.times do |n_row|
          row = dm.get_row(n_row)
          # Inner loop obtaining the value using Gda::DataModel#get_value.
          dm.n_columns.times do |n_col|
              val = row.get_value(n_col)
              puts val.to_s
          end
      end
  end

== Example using 'The Ruby Way'

For your convenience, Ruby/Libgda provides a more rubish way to access values in data models.

Just have a look at the following methods: Gda::DataModel#each_column, Gda::DataModel#columns, Gda::DataModel#each_row, Gda::DataModel#rows, Gda::Row#each_value and Gda::Row#values.

Here is an example using iterators:

  def show_table3(dm)
      dm.each_column { |title| puts title }
      puts ""
      dm.each_row { |row| row.each_value { |val| puts val.to_s } }
  end

Life is good, isn't it? ;-)





ruby-gnome2-cvs メーリングリストの案内
Back to archive index