Managing data models

Each time we execute a normal query, we will obtain a GdaDataModel object, which is the way to see what the query returned. As GdaDataModel is an object, we can manage it with GdaDataModel class.

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.

Let's see the functions we need:

This functions are very easy to use, so let's see some clear examples:


Example using direct cell access

This function accesses the data model by directly accessing cells (using gda_data_model_get_value_at () )

          void
          show_table (GdaDataModel * dm)
          {
            gint row_id;
            gint column_id;
            GdaValue *value;
          
(1)            for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                 column_id++)
              g_print("%s\t",gda_data_model_get_column_title (dm, column_id));
            g_print("\n");
          
(2)            for (row_id = 0; row_id < gda_data_model_get_n_rows (dm); row_id++)
              {
                for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                     column_id++)
                  {
(3)                    value =
                      (GdaValue *) gda_data_model_get_value_at (dm, column_id, row_id);
                    g_print ("%s\t", gda_value_stringify (value));
                  }
                g_print("\n");
              }
          }
          
(1)
Loop for writing column names.
(2)
Double loop accessing values using gda_data_model_get_value_at ()
(3)
Data returned is a GdaValue object.

Example using row access

This function accesses the data model by accessing rows (using gda_data_model_get_row () and gda_row_get_value () )

            
          void
          show_table2 (GdaDataModel * dm)
          {
            gint row_id;
            gint column_id;
            GdaValue *value;
            GdaRow *row;
          
(1)            for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                 column_id++)
              g_print("%s\t",gda_data_model_get_column_title (dm, column_id));
            g_print("\n");
          
(2)            for (row_id = 0; row_id < gda_data_model_get_n_rows (dm); row_id++)
              {
                row = (GdaRow *) gda_data_model_get_row (dm, row_id);
(3)                for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                     column_id++)
                  {
		    /* We don't need to free this value! */
                    value = gda_row_get_value (row, column_id);
                    string=gda_value_stringify (value);
                    g_print ("%s\t", string);
                    g_free(string);
                  }
                g_print ("\n");
              }
          }
          
(1)
Loop for writing column names.
(2)
Outer loop obtaining rows using gda_data_model_get_row ()
(3)
Inner loop obtaining the value using gda_row_get_value () . Notice that gda_row_get_value () doesn't return a const GdaValue, so we have to free it.

Freeing data models

When you finish using data models you must free it, but GdaDataModel class does not have a function to do it, so you have to use g_object_unref ().