1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/*
Compile this like so, or similar:
gcc postgres_gda_test.c `pkg-config libgda --libs --cflags`
murrayc
*/
#include <libgda/libgda.h>
/* Show errors from a working connection */
static void
get_errors (GdaConnection *connection)
{
GList *list;
GList *node;
GdaError *error;
list = (GList *) gda_connection_get_errors (connection);
for (node = g_list_first (list); node != NULL; node = g_list_next (node)) {
error = (GdaError *) node->data;
g_print ("Error no: %d\t", gda_error_get_number (error));
g_print ("desc: %s\t", gda_error_get_description (error));
g_print ("source: %s\t", gda_error_get_source (error));
g_print ("sqlstate: %s\n", gda_error_get_sqlstate (error));
}
}
int
main(int argc, char *argv[])
{
const gchar* connection_string = "HOST=localhost;USER=murrayc;PASSWORD=yourpasswordhere;DATABASE=template1";
GdaClient *client = 0;
GdaConnection *con = 0;
gboolean errors = FALSE;
gint rows;
gda_init ("glom-gda-test", NULL, argc, argv);
/* 3. Create a gda client */
client = gda_client_new ();
/* 4. Open the connection */
con = gda_client_open_connection_from_string (client, "PostgreSQL", connection_string, 0);
if (!GDA_IS_CONNECTION (con)) {
g_print ("** ERROR: could not open connection.\n");
/* This cannot work because it needs a working connection: get_errors (con); */
return 0;
}
gboolean created = gda_connection_create_database(con, "glomtest");
if(!created)
{
g_print("** Error: gda_connection_create_database failed.\n");
get_errors(con);
}
gda_connection_close (con);
g_object_unref (G_OBJECT (client));
g_print ("** Connection successfully opened, database created, and connection closed.\n");
return 0;
}
|