![]() | ![]() | GNOME Data Access manual | ![]() |
---|
Table of Contents
Before invoking a query you have to build the structure containing the command and you can do this with <LINK> <EMPHASIS>gda_command_new ()</EMPHASIS></LINK>.
The command type we most commonly use is <LINK>GDA_COMMAND_TYPE_SQL</LINK> because we will only focus on SQL queries[3]
<PROGRAMLISTINGCO> <AREASPEC> <AREA></AREA> <AREA></AREA> </AREASPEC>typedef enum { GDA_COMMAND_OPTION_IGNORE_ERRORS = 1, GDA_COMMAND_OPTION_STOP_ON_ERRORS = 1 << 1, GDA_COMMAND_OPTION_BAD_OPTION = 1 << 2 } <LINK>GdaCommandOptions</LINK>;<CALLOUTLIST> <CALLOUT> <PARA> Ignores all errors and executes all sentences returning data models. For failed sentences, it returns an empty data model. </PARA> </CALLOUT> <CALLOUT>
Stops when finding and error and doesn't return data models.
</CALLOUT> </CALLOUTLIST> </PROGRAMLISTINGCO>Here you see an example of creating a command:
<PROGRAMLISTINGCO> <AREASPEC> <AREA></AREA> <AREA></AREA> <AREA></AREA> </AREASPEC>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); }<CALLOUTLIST> <CALLOUT> <PARA> Our function. You can give it several comma-separated sentences. </PARA> </CALLOUT> <CALLOUT>
We will see it <LINK>later</LINK>.
</CALLOUT> <CALLOUT>It is a good practice to free the commands.
</CALLOUT> </CALLOUTLIST> </PROGRAMLISTINGCO><EMPHASIS>Non queries</EMPHASIS> are queries that does not return data, only the number of rows affected, as a DELETE or an UPDATE. We use <LINK><EMPHASIS> gda_connection_execute_non_query()</EMPHASIS></LINK>
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); }
Normal queries are queries that return data (<LINK>data models</LINK>). You have two ways to do this:
<ITEMIZEDLIST> <LISTITEM><LINK> gda_data_model_execute_single_command()</LINK>
</LISTITEM> <LISTITEM><LINK> gda_data_model_execute_command()</LINK>
</LISTITEM> </ITEMIZEDLIST>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 <LINK> gda_connection_execute_single_command ()</LINK>. Here you see an example:
<PROGRAMLISTINGCO> <AREASPEC> <AREA></AREA> <AREA></AREA> </AREASPEC>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_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); }<CALLOUTLIST> <CALLOUT> <PARA> Executes the query and obtains a list of <LINK>data models</LINK> </PARA> </CALLOUT> <CALLOUT> <PARA> Loop for moving through the list of data models. If you use <LINK> gda_connection_execute_single_command()</LINK>, you should not need to use a loop, because this function would return a <LINK>data model</LINK>. </PARA> </CALLOUT> </CALLOUTLIST> </PROGRAMLISTINGCO>
<< Connecting | Managing data models >> |