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

- gda_data_model_get_n_rows()
- gda_data_model_get_n_columns()
- gda_data_model_describe_column()
- gda_data_model_get_column_title()
- gda_data_model_get_column_position()
- gda_data_model_get_row()
- gda_data_model_get_value_at()
- gda_row_get_value()

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;

                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");

                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++)
                      {
                        value =
                          (GdaValue *) gda_data_model_get_value_at (dm, column_id, row_id);
                        g_print ("%s\t", gda_value_stringify (value));
                      }
                    g_print("\n");
                  }
              }

  Loop for writing column names.
  Double loop accessing values using gda_data_model_get_value_at ()
  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;

                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");

                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);
                    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");
                  }
              }

  Loop for writing column names.
  Outer loop obtaining rows using gda_data_model_get_row ()
  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 ().