Executing queries

Building commands

Before invoking a query you have to build the structure containing the command and you can do this with gda_command_new ().

The command type we most commonly use is GDA_COMMAND_TYPE_SQL because we will only focus on SQL queries[1]

          typedef enum {
          GDA_COMMAND_OPTION_IGNORE_ERRORS  = 1,
          GDA_COMMAND_OPTION_STOP_ON_ERRORS = 1 << 1,
          GDA_COMMAND_OPTION_BAD_OPTION     = 1 << 2
          } GdaCommandOptions;
        

1

Ignores all errors and executes all sentences returning data models. For failed sentences, it returns an empty data model.

2

Stops when finding and error and doesn't return data models.

Here you see an example of creating a command:

          gint
          execute_sql_non_query (GdaConnection *connection, const gchar * buffer)
          {
          GdaCommand *command;
          gint number;
          
          command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
          number  = gda_connection_execute_non_query (connection, command, NULL);
          
          gda_command_free (command);
          
          return (number);
          }
        

1

Our function. You can give it several comma-separated sentences.

2

We will see it later.

3

It is a good practice to free the commands.

Making non queries

Non queries are queries that does not return data, only the number of rows affected, as a DELETE or an UPDATE. We use gda_connection_execute_non_query()

        gint
        execute_sql_non_query (GdaConnection *connection, const gchar * buffer)
        {
        GdaCommand *command;
        gint number;
        
        command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
        number  = gda_connection_execute_non_query (connection, command, NULL);
        
        gda_command_free (command);
        
        return (number);
        }
      

Making normal queries

Normal queries are queries that return data (data models). You have two ways to do this:

You can use the first way when you want to invoke only a single command. Second way is used to execute several comma-separated sentences. It is recommended to use gda_connection_execute_single_command (). Here you see an example:

          gboolean
          execute_sql_command (GdaConnection *connection, const gchar * buffer)
          {
          GdaCommand *command;
          GList *list;
          GList *node;
          gboolean errors=FALSE;
          
          GdaDataModel *dm;
          
          
          command = gda_command_new (buffer, GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
          list = gda_connection_execute_select_command (connection, command, NULL);
          if (list!=NULL)
          for (node=g_list_first(list); node != NULL; node=g_list_next(node))
          {
          dm=(GdaDataModel *) node->data;
          if (dm == NULL)
          {
          errors=TRUE;
          }
          else
          {
          show_table (dm);
          g_object_unref(dm);
          }
          }
          else
          {
          errors=TRUE;
          }
          gda_command_free (command);
          
          return (errors);
          }
        

1

Executes the query and obtains a list of data models

2

Loop for moving through the list of data models. If you use gda_connection_execute_single_command(), you should not need to use a loop, because this function would return a data model.



[1] There are other command types, as XML and so on.