![]() |
![]() |
![]() |
GNOME Data Access 4.0 manual | ![]() |
---|
Any query within libgda is represented as a GdaStatement object. Each GdaStatement object can store exactly one SQL statement (SQL statements are generally separated by semi-colons). Several statements can be groupped into a GdaBatch object.
A GdaStatement object can contain variables, and a value needs to be assigned to each one before the query can be run; the same query object can then be run as a prepared statement, only binding variables each time.
GdaStatement objects can be created by:
parsing SQL code using gda_sql_parser_parse_string () from a GdaSqlParser object.
building a GdaSqlStatement structure and creating a GdaStatement object around that structure. This method requires more knowledge but is more portable across databases
Executing a statement is a matter of calling gda_connection_statement_execute () or one of its simplified versions if the nature of the statement (SELECT or not) is known.
The following example shows how to use a GdaStatement to list the details of some data while making a variable (named "the_id") vary from 0 to 9 (for simplicity, error checking has been removed):
GdaConnection *cnc; GdaSqlParser *parser; GdaStatement *stmt; GdaSet *params; GdaHolder *p; GValue *value; gint i; cnc = ...; [...] stmt = gda_sql_parser_parse_string (parser, "SELECT * FROM customers WHERE id=##the_id::gint", NULL, NULL); gda_statement_get_parameters (stmt, ¶ms, NULL); p = gda_set_get_holder (params, "the_id"); value = gda_value_new (G_TYPE_INT); for (i = 0; i < 10; i++) { GdaDataModel *res; g_value_set_int (value, i); gda_holder_set_value (p, value); res = gda_connection_statement_execute_select (cnc, stmt, params, NULL); gda_data_model_dump (res, stdout); g_object_unref (res); } g_object_unref (params); g_object_unref (stmt);