[gnome-db] Compiling/Running a simple gnome-db program
- From: Daniel Morgan <danmorg sc rr com>
- To: GDA <gnome-db-list gnome org>
- Subject: [gnome-db] Compiling/Running a simple gnome-db program
- Date: 30 Jan 2002 11:16:34 -0500
My simple gnome-db program keeps giving me a segfault. What am I doing
wrong?
I have maked and installed libgda 0.2.94 and gnome-db 0.2.94 on Red Hat
7.2.
I have been succuessful at creating and running my simple libgda
programs, but I am having difficulty with my simple gnome-db program.
I have attached the source file and make file gnomedb-hello.c and
gnomedb-hello.make.
Thanks in advance,
Daniel
/*******************************************************
** gdahello.c - sample libgda program
**
** Need to have or create a table called nametypes with
** two columns called nametype and description.
**
********************************************************/
#include <gnome-db.h>
#define MAXSTRLENGTH 1024
#define VERSION "0.2.94"
gint delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
/* If you return FALSE in the "delete_event" signal handler,
* GTK will emit the "destroy" signal. Returning TRUE means
* you don't want the window to be destroyed.
* This is useful for popping up 'are you sure you want to quit?'
* type dialogs. */
g_print ("delete event occurred\n");
/* Change TRUE to FALSE and the main window will be destroyed with
* a "delete_event". */
return(FALSE);
}
void intro()
{
g_print("GNOME-DB Hello %s\n", VERSION);
g_print("Copyright (c) 2000 Free Software Foundation, Inc.\n");
g_print("This program is part of GNU Data Access (GDA) version %s.\n", VERSION);
g_print("gnomedb-hello comes with NO WARRANTY, ");
g_print("to the extent permitted by law.\n");
g_print("You may redistribute copies of ");
g_print("gnomedb-hello under the terms of the GNU\n");
g_print("General Public License. ");
g_print("For more information see the file COPYING.\n");
}
/* ------------------------------------------------------------------------- */
/* Print errors and exit program */
/* ------------------------------------------------------------------------- */
int die(GdaConnection *cnc)
{
GList *errors;
GList *node;
GdaError *error;
g_print("\nthis program died because of a GDA error\n");
errors = gda_connection_get_errors(cnc);
for (node = g_list_first(errors); node; node = g_list_next(node)) {
error = (GdaError *) node->data;
g_print("%s\n", gda_error_get_description(error));
}
gda_error_list_free(errors);
exit(1);
}
int show_gnomedb_gui(GdaRecordset *rs)
{
GtkWidget* window; /* a window */
GtkWidget* grid; /* a data grid */
/* Create the window. */
g_print("\ngtk_window_new\n");
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_print("\ngtk_window_default_size\n");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
/* Set up the event handler */
g_print("\ngtk_signal_connect\n");
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (delete_event), NULL);
/* Create the grid and add it to the window. */
g_print("\ngnome_db_grid\n");
grid = gnome_db_grid_new(rs);
g_print("\ngtk_container_add\n");
gtk_container_add(GTK_CONTAINER(window), grid);
/* Show the window and its widget */
g_print("\ngtk_widget_show_all\n");
gtk_widget_show_all(window);
g_print("\ngtk_main\n");
/* Initiate the event loop */
gtk_main();
g_print("\nleaving show_gnomedb_gui\n");
return(0);
}
/* ------------------------------------------------------------------------- */
/* List all providers */
/* ------------------------------------------------------------------------- */
char *list_providers()
{
GList *list;
GList *node;
GdaProvider *provider;
int i = 0;
char *selected;
char number[4]; /* the number of provider entered, I think
that we will have not more than 999
providers */
gint pno; /* provider number converted with atoi */
list = gda_provider_list();
if (!list) {
g_print("\n*** Error ***\n");
g_print("There are no GDA providers available.\n");
g_print("If you installed libgda from a RPM, a DEB, or a Linux\n");
g_print("Distribution, you should install one of the providers,\n");
g_print("which you can get from the same source as you got libgda.\n");
g_print("If you built libgda yourself, you should run ./configure\n");
g_print("with one of the options that enable the providers (run\n");
g_print("./configure --help for details) and then make and install\n");
g_print("again.\n");
g_print("If you already installed a provider, the .oafinfo file is\n");
g_print("maybe not installed in the right directory.\n");
exit(1);
}
g_print("\nThe following %d GDA providers are available:\n", g_list_length(list));
for (node = g_list_first(list); node; node = g_list_next(node)) {
provider = (GdaProvider *) node->data;
g_print("%d: %s\n", ++i, GDA_PROVIDER_NAME(provider));
}
if (i > 1) {
do {
g_print("\nChoose one (enter the number):");
fgets(number, sizeof(number), stdin);
pno = atoi(number);
} while (pno < 1 || pno > i);
node = g_list_nth(list, pno - 1);
} else {
node = g_list_first(list);
}
provider = (GdaProvider *) node->data;
selected = g_strdup(GDA_PROVIDER_NAME(provider));
gda_provider_free_list(list);
return (selected);
}
/* ------------------------------------------------------------------------- */
/* List all tables for a connection */
/* ------------------------------------------------------------------------- */
void list_tables(GdaConnection * cnc)
{
GdaRecordset *rs;
GdaField *field;
gint i;
g_print("\nopening table schema...\n");
/* open the table schema */
rs = gda_connection_open_schema(cnc,
GDA_Connection_GDCN_SCHEMA_TABLES,
GDA_Connection_no_CONSTRAINT);
if (!rs)
die(cnc);
g_print("\nThis database has following tables:\n");
/* for each record in recordset... */
for (gda_recordset_move_first(rs); !gda_recordset_eof(rs);
gda_recordset_move_next(rs)) {
/* loop through the fields in a record and printing the values */
for (i = 0; i < gda_recordset_rowsize(rs); i++) {
field = gda_recordset_field_idx(rs, i);
g_print("%s=%s\t", gda_field_get_name(field),
gda_stringify_value(NULL, 0, field));
}
g_print("\n");
}
/* close recorset */
gda_recordset_free(rs);
}
void list_nametypes(GdaConnection * cnc)
{
GdaRecordset *rs;
GdaField *description;
GdaCommand *cmd;
gulong rc;
/* Create a command */
cmd = gda_command_new();
/* set the connection to the command object */
gda_command_set_connection(cmd, cnc);
/* set the SQL statement string to the command object */
gda_command_set_text(cmd, "SELECT description FROM nametypes");
/* Execute the command */
rs = gda_command_execute((GdaCommand *)cmd, &rc, (gulong) 0UL);
/* gda error handling */
if (!rs)
die(cnc);
/* Display the results. */
gda_recordset_move_first(rs);
while (!gda_recordset_eof (rs)) {
/* Retrieve the name field. */
description = gda_recordset_field_name(rs, "description");
g_print ("NameType = %s\n", gda_stringify_value(NULL, 0, description));
/* Move to the next row. */
gda_recordset_move_next (rs);
}
/* close recorset */
gda_recordset_free(rs);
/* close command */
gda_command_free((GdaCommand *)cmd);
}
void show_nametypes(GdaConnection * cnc)
{
GdaRecordset *rs;
/* GdaField *description; */
GdaCommand *cmd;
gulong rc;
/* Create a command */
g_print("\ngda_command_new\n");
cmd = gda_command_new();
/* set the connection to the command object */
g_print("\ngda_command_set_connection\n");
gda_command_set_connection(cmd, cnc);
/* set the SQL statement string to the command object */
g_print("\ngda_command_set_text\n");
gda_command_set_text(cmd, "SELECT description FROM nametypes");
/* Execute the command */
g_print("\ngda_command_execute\n");
rs = gda_command_execute((GdaCommand *)cmd, &rc, (gulong) 0UL);
/* gda error handling */
if (!rs)
die(cnc);
g_print("\ngoing into show_gnomedb_gui()\n");
show_gnomedb_gui(rs);
/* close recorset */
g_print("\ngda_recordset_free\n");
gda_recordset_free(rs);
/* close command */
g_print("gda_command_free\n");
gda_command_free(cmd);
}
/* ------------------------------------------------------------------------- */
/* Main function */
/* ------------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
gchar *pg_provider = "OAFIID:GNOME_GDA_Provider_Postgres_ConnectionFactory";
GdaConnection *cnc;
gchar dsn[MAXSTRLENGTH] = "DATABASE=test";
gchar user[MAXSTRLENGTH] = "userid";
gchar password[MAXSTRLENGTH] = "password";
intro();
g_print("\ninitializing...\n");
/* gda_init("gda-test", NULL, argc, argv); */
g_print("\ngnome_db_init\n");
gnome_db_init("db_connect", "0.1", argc, argv);
/* get new instance of a connection object */
g_print("\ngda_connection_new\n");
cnc = gda_connection_new(gda_corba_get_orb());
/* provider = list_providers(); */
g_print("\n print the provider that will be used.\n");
g_print("\n Using provider: %s...\n", pg_provider);
/* set provider */
g_print("\ngda_connection_set_provider\n");
gda_connection_set_provider(cnc, pg_provider);
g_print("\nopening connection...\n");
g_print("\n Using connection data: %s %s %s\n",dsn,user,password);
/* connect to the database */
if(gda_connection_open(cnc, dsn, user, password) == -1)
die(cnc);
/* Check the connection. */
if ( !gda_connection_is_open(cnc) ) {
g_print("Connection failed.\n");
die(cnc);
} else {
g_print("Connection succeeded.\n");
gda_connection_close(cnc);
}
g_print("\nassuming connection was successful\n");
/* list_tables(cnc); */
g_print("\ngoing into show_nametypes\n");
show_nametypes(cnc);
g_print("\nclosing connection...\n");
/* close connection - free connection object */
gda_connection_free(cnc);
return (0);
}
PROJECT = gnomedb-hello
CC = gcc
OBJS = gnomedb-hello.c
CFLAGS = `gnomedb-config --cflags`
LIBS = `gnomedb-config --libs`
$(PROJECT): $(OBJS)
$(CC) -Wall -g $(CFLAGS) $(OBJS) -o $(PROJECT) $(LIBS)
clean:
rm -f *.o $(PROJECT)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]