Re: [gnome-db] gdaquery



On 6/19/07, Andrea Zagli <azagli inwind it> wrote:
i'm trying the new libgda-3

my problem is that i didn't understand very well how gdaquery and gdaqueryfield joins together

this is the code

#include <libgda/libgda.h>

int
main (int argc, char **argv)
{
        GdaClient *client;
        GdaConnection *conn;
        GdaDict *dict;
        GdaQuery *qu;
        GdaObject *dm;
        GError *error = NULL;

        gda_init ("test", "1.0", argc, argv);

        client = gda_client_new ();

        conn = gda_client_open_connection_from_string (client, "SQLite", "DB_DIR=/media/disk/libgda3_gdaquery;DB_NAME=test.db", NULL, NULL, 0, &error);

        if (conn == NULL)
                {
                        g_fprintf (stderr, "Errore apertura connessione.\n%s\n", error->message);
                }
        else
                {
                        GdaQueryTarget *qutarget;
                        GdaQueryField *qufield;
                        GdaQueryField *quvalue;
                        GValue *value;

                        value = g_malloc0 (sizeof (GValue));

                        error = NULL;
                        dict = gda_dict_new ();
                        gda_dict_set_connection (GDA_DICT (dict), conn);

                        qu = gda_query_new (GDA_DICT (dict));
                        if (qu == NULL)
                                {
                                        g_fprintf (stderr, "Errore in gda_query_new.\n");
                                }

                        gda_query_set_query_type (GDA_QUERY (qu), GDA_QUERY_TYPE_UPDATE);

                        qutarget = gda_query_target_new (GDA_QUERY (qu), "appo");

                        qufield = gda_query_field_field_new (GDA_QUERY (qu), "ciccio");
                        gda_query_field_set_visible (GDA_QUERY_FIELD (qufield), TRUE);

                        quvalue = gda_query_field_value_new (GDA_QUERY (qu), G_TYPE_STRING);
                        g_value_init (value, G_TYPE_STRING);
                        g_value_set_string (value, "paperino");
                        gda_query_field_value_set_value (GDA_QUERY_FIELD_VALUE (quvalue), value);
                        gda_query_field_set_visible (GDA_QUERY_FIELD (quvalue), TRUE);

                        g_object_set (qufield,
                                      "target", qutarget,
                                      "target-id", "appo",
                                      "target-name", "appo",
                                      "value-provider", quvalue,
                                      NULL);

                        if (!gda_query_add_target (GDA_QUERY (qu), GDA_QUERY_TARGET (qutarget), &error))
                                {
                                        g_fprintf (stderr, "Errore in add_target.\n%s\n", error->message);
                                }

                        error = NULL;
                        dm = gda_query_execute (GDA_QUERY (qu), NULL, FALSE, &error);
                        if (error != NULL)
                                {
                                        g_fprintf (stderr, "Errore in esecuzione query.\n%s\n", error->message);
                                }

                        g_fprintf (stderr, "SQL: %s\n", gda_query_get_sql_text (GDA_QUERY (qu)));
                }


        return 0;
}

when i execute it, it returns an error on gda_query_execute because the query's sql is wrong; in fact gda_query_get_sql_text returns "update appo set "

instead I thought to have created "update appo set ciccio='paperino'"


You need to add the qufield and quvalue to the query, using
gda_entity_add(). I agree that it's not easy to find, and I probably
will add a gda_query_add_field() function in the future.

However, except for testing and learning purposes, I suggest you use
the gda_query_new_from_sql function which will do this for you in one
step.

Here is attached my modified version with some comments.

Regards,

Vivien
#include <libgda/libgda.h>

int
main (int argc, char **argv)
{
	GdaClient *client;
	GdaConnection *conn;
	GdaDict *dict;
	GdaQuery *qu;
	GdaObject *dm;
	GError *error = NULL;

	gda_init ("test", "1.0", argc, argv);

	client = gda_client_new ();

	conn = gda_client_open_connection_from_string (client, "SQLite", "DB_DIR=.;DB_NAME=Test.db", NULL, NULL, 0, &error);

	if (conn == NULL) {
		g_fprintf (stderr, "Errore apertura connessione.\n%s\n", error->message);
	}
	else {
		GdaQueryTarget *qutarget;
		GdaQueryField *qufield;
		GdaQueryField *quvalue;
		GValue *value;

		value = g_malloc0 (sizeof (GValue));

		error = NULL;
		dict = gda_dict_new ();
		gda_dict_set_connection (GDA_DICT (dict), conn);

		qu = gda_query_new (GDA_DICT (dict));
		/* REM: qu wil _never_ be NULL at this point, so no need to test it */
		if (qu == NULL)	{
			g_fprintf (stderr, "Errore in gda_query_new.\n");
		}

		gda_query_set_query_type (GDA_QUERY (qu), GDA_QUERY_TYPE_UPDATE);

		/* target */
		qutarget = gda_query_target_new (GDA_QUERY (qu), "appo");
		if (!gda_query_add_target (GDA_QUERY (qu), GDA_QUERY_TARGET (qutarget), &error)) {
			g_fprintf (stderr, "Errore in add_target.\n%s\n", error->message);
			g_error_free (error);
			error = NULL;
			exit (1);
		}
		g_object_unref (qutarget); /* don't forget to unref at this point unless you really want to keep
					      a reference on the object */

		/* ciccio field */
		qufield = gda_query_field_field_new (GDA_QUERY (qu), "ciccio");
		gda_query_field_set_visible (GDA_QUERY_FIELD (qufield), TRUE);
		gda_entity_add_field (GDA_ENTITY (qu), GDA_ENTITY_FIELD (qufield));
		g_object_unref (qufield);/* don't forget to unref at this point unless you really want to keep
					      a reference on the object */

		/* value field */
		quvalue = gda_query_field_value_new (GDA_QUERY (qu), G_TYPE_STRING);
		gda_query_field_set_visible (GDA_QUERY_FIELD (quvalue), FALSE);
		gda_entity_add_field (GDA_ENTITY (qu), GDA_ENTITY_FIELD (quvalue));
		g_object_unref (quvalue);/* don't forget to unref at this point unless you really want to keep
					      a reference on the object */

		g_value_init (value, G_TYPE_STRING);
		g_value_set_string (value, "paperino");
		gda_query_field_value_set_value (GDA_QUERY_FIELD_VALUE (quvalue), value);

		g_object_set (qufield,
			      "target", qutarget,
			      "target-id", "appo",
			      "target-name", "appo",
			      "value-provider", quvalue,
			      NULL);


		dm = gda_query_execute (GDA_QUERY (qu), NULL, FALSE, &error);
		if (error != NULL) {
			g_fprintf (stderr, "Errore in esecuzione query.\n%s\n", error->message);
		}

		g_fprintf (stderr, "SQL: %s\n", gda_query_get_sql_text (GDA_QUERY (qu)));
	}


	return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]