File: postgres_gda_test.c

package info (click to toggle)
glom 1.30.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,260 kB
  • sloc: ansic: 160,257; cpp: 72,338; javascript: 9,331; sh: 4,971; xml: 476; makefile: 315; perl: 236
file content (69 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download | duplicates (3)
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;
}