Re: [gnome-db] libgda cursor support
- From: Murray Cumming <murrayc murrayc com>
- To: Vivien Malerba <vmalerba gmail com>
- Cc: gnome-db-list <gnome-db-list gnome org>
- Subject: Re: [gnome-db] libgda cursor support
- Date: Tue, 25 Sep 2007 20:31:00 +0200
On Wed, 2007-09-19 at 14:10 +0200, Vivien Malerba wrote:
> This means that the total number of rows of the returned data model is
> not yet known, you need to iterate over it to reach the end, at which
> time the total number of rows will be known and
> gda_data_model_get_n_rows() will return something >= 0.
That doesn't seem to be working. Am I missing something in the attached
test code? It gives me this output:
murrayc murrayc-desktop:~$ ./a.out
row=0
value=NULL
row=1
value=NULL
row=2
value=NULL
row=3
value=NULL
gda_data_model_get_n_rows()=-1 after iterating.
--
murrayc murrayc com
www.murrayc.com
www.openismus.com
//Compile with
//g++ libgda_cursor_test.c `pkg-config libgda-3.0 --libs --cflags`
#include <libgda/libgda.h>
int
main (int argc, char **argv)
{
gda_init ("test", "0.1", argc, argv);
GdaClient* client = gda_client_new();
GError* error = NULL;
GdaConnection* connection = gda_client_open_connection_from_string (client, "PostgreSQL", "HOST=localhost;DB_NAME=glom_musiccollection21", "murrayc" /* username */, "something" /* password */, (GdaConnectionOptions)0, &error);
if(error)
{
printf("GError from gda_client_open_connection_from_string(): %s\n", error->message);
g_error_free(error);
return EXIT_FAILURE;
}
GdaCommand* command = gda_command_new("SELECT * FROM artists", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS);
GdaParameterList *params = gda_parameter_list_new(NULL);
GValue value = { 0, };
g_value_init(&value, G_TYPE_BOOLEAN);
g_value_set_boolean(&value, TRUE);
gda_parameter_list_add_param_from_value (params, "ITER_MODEL_ONLY", &value);
GdaDataModel* model = gda_connection_execute_select_command(connection, command, params, &error);
if(error)
{
printf("GError from gda_connection_execute_select_command(): %s\n", error->message);
g_error_free(error);
return EXIT_FAILURE;
}
if(!model)
{
printf("gda_connection_execute_select_command(): returned NULL but did not set the GError.\n");
return EXIT_FAILURE;
}
GdaDataModelIter *iter = gda_data_model_create_iter(model);
while(gda_data_model_iter_move_next(iter))
{
const int row = gda_data_model_iter_get_row(iter);
printf("row=%d\n", row);
const GValue *value = gda_data_model_get_value_at(model, 0, row);
gchar *text = gda_value_stringify(value);
printf (" value=%s\n", text);
g_free(text);
}
gint count_rows = gda_data_model_get_n_rows(model);
if(count_rows < 0)
{
//This happens when we use the ITER_MODEL_ONLY parameter:
printf("gda_data_model_get_n_rows()=%d after iterating.\n", count_rows);
return EXIT_FAILURE;
}
printf ("Number of columns: %d, Number of rows=%d\n", gda_data_model_get_n_columns(model), count_rows);
gda_command_free (command);
g_object_unref (params);
g_object_unref (connection);
g_object_unref (client);
return EXIT_SUCCESS;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]