Connecting

libgda allows data sources (DSN) to be defined and refered to by a unique name which contains all the required information to actually open a connection (except the name and password if they are required). Of course it's still possible to open a connection without having defined a DSN, in which case a connection string is used to specify all the parameters resuired to open a connection. For more information about connection strings, see the gda_client_open_connection_from_string ()'s documentation.

Connections are created by a GdaClient object which can also manage connection pools. So the first step is to create a GdaClient object using gda_client_new (), and then use gda_client_open_connection () (or gda_client_open_connection_from_string () for connections not defined as DSN) to create connection objects (GdaConnection objects). Each connection object can then be used to actually execute queries, for example:

void
do_stuff () {
	GdaClient *client;
	GdaConnection *connection;
      
        /* open a connection */
	client = gda_client_new ();      
	g_print ("CONNECTING\n");
	connection = gda_client_open_connection (client, "calvaris", NULL, NULL,
						 GDA_CONNECTION_OPTIONS_READ_ONLY);
	g_print ("CONNECTED\n");
      
        /* use the connection */
	execute_some_queries (connection);
      
        /* close the connection */
        g_object_unref (G_OBJECT (connection));
	g_object_unref (G_OBJECT (client));
}
      

Closing the connection can be ordered using gda_connection_close (), or is automatically done when the connection object is destroyed (as is the case in the example abive when g_object_unref() is called with the connection as argument).