Save the following code to the (for example) sample.c file and compile with:

cc -Wall -o sample sample.c `pkg-config --cflags --libs libgda-ui-4.0`

Run the ./sample program without any argument. The SQL run in this example selects all the fields of the customers table, so you should use a database which has a customers table (the default SalesTest DSN installed by Libgda the first time you run an application using Libgda will work).

#include <libgda.h>
#include <libgda-ui/libgda-ui.h>

int
main (int argc, char *argv[])
{
        gtk_init (&argc, &argv);
        gdaui_init ();

        /* create a login dialog window to let the user select a data source (or declare a new one)
         * and specify a username and password if necessary
         */
        GtkWidget *window, *login;
        window = gtk_dialog_new_with_buttons ("Select the Data Source to connect to",
                                              NULL,
                                              0,
                                              GTK_STOCK_CANCEL,
                                              GTK_RESPONSE_NONE,
                                              GTK_STOCK_OK,
                                              GTK_RESPONSE_OK,
                                              NULL);

        /* Create the login widget */
        login = gdaui_login_new (NULL);
        gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
                            login, TRUE, TRUE, 0);
        gtk_widget_show (login);

        if (gtk_dialog_run (GTK_DIALOG (window)) != GTK_RESPONSE_OK) {
                g_print ("Cancelled!\n");
                exit (0);
        }

        /* open the connection */
        const GdaDsnInfo *dsninfo;
        GdaConnection *cnc;
        GError *error;

        dsninfo = gdaui_login_get_connection_information (GDAUI_LOGIN (login));
        cnc = gda_connection_open_from_string (dsninfo->provider,
                                               dsninfo->cnc_string,
                                               dsninfo->auth_string,
                                               GDA_CONNECTION_OPTIONS_NONE, &error);

        gtk_widget_destroy (window);

        if (!cnc) {
                g_print ("Could not open connection: %s\n",
                         error && error->message ? error->message : "No detail");
                exit (1);
        }

        /* execute a SELECT statement; here the easiest API is used */
        GdaDataModel *data_model;
        data_model = gda_execute_select_command (cnc, "SELECT * FROM customers", &error);
        if (!data_model) {
                g_print ("Could not execute the SQL command: %s\n",
                         error && error->message ? error->message : "No detail");
                /* close the connection */
                g_object_unref (cnc);
                exit (1);
        }
        
        /* Create a main window and show the data model in a grid */
        GtkWidget *grid;
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        grid = gdaui_grid_new (data_model);
        /*g_object_set (G_OBJECT (grid), "info-flags",
          GDAUI_DATA_PROXY_INFO_CURRENT_ROW | GDAUI_DATA_PROXY_INFO_ROW_MODIFY_BUTTONS, NULL);*/
        g_object_unref (data_model);
        gtk_container_add (GTK_CONTAINER (window), grid);
        gtk_widget_show_all (window);

        gtk_main();

        g_object_unref (cnc);

        return 0;
}

CodeExampleGrid (last edited 2010-07-01 20:46:29 by VivienMalerba)