only for RuBoard - do not distribute or recompile | |
The Data Display Control (DDC)
This control will show one table at a time. When given an SQL select string, it dynamically generates a CList widget. It is not completely independent of the current project, but it could be with a little work. The code for the DDC is in a separate file, called ddc.c (and, of course, ddc.h ). Listing 9.1 is ddc.c .
Listing 9.1 Data Display Control (DDC) Code
#ifdef HAVE_CONFIG_H # include <config.h> #endif #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <gdk/gdkkeysyms.h> #include <gtk/gtk.h> #include <mysql.h> #include "callbacks.h" #include "interface.h" #include "support.h" #include "comm_utils.h" GtkWidget *frm_table_display; GtkWidget *create_ddc (gchar *table_name) { /* This widget will create and return a scrolledwindow widget, * inside of which is the CList widget with the data. Obviously, * to make this more project-independent, such things as the * database connection would also need to be passed in as * parameters. */ MYSQL *conx; //The connection to the database. GtkWidget *scrolledwindow1; //Container for clist_table1. GtkWidget *clist_table; //Table to display the data. GtkWidget *label; gint counter; gint cols = 3; //Default value for number of columns. gchar *sql; //Generic value for sql string. MYSQL_RES *result_set; //Holds data returned from MySQL. MYSQL_ROW db_row; //A single row from result_set. MYSQL_FIELD *field; //A single field from db_row. /* row[20] is a placeholder, but the subscript needs to * be larger than the maximum number of columns in any * of the possible tables this could select from. In this * case, because the structure of the commish database is * known and controlled, 20 has a reasonable chance of * working for the foreseeable future. */ gchar *row[20] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}; /* Create a new scrolled window, into which the CList * widget will be added. */ scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL); gtk_widget_show (scrolledwindow1); conx = mysql_init(0L); if (conx == 0L) { g_print("mysql_init failure...\n"); return 0L; } mysql_real_connect (conx, "einstein", "com_user", "syL0U812", "commish", 0, 0L, 0); if (conx == 0L) { g_print("mysql_real_connect failure...\n"); return 0L; } sql = g_strconcat("select * from ", table_name, 0L); g_print("sql is: %s\n", sql); if (mysql_query (conx, sql) != 0) { g_print("query failure...\n"); return 0L; } result_set = mysql_store_result (conx); /* Find out how many columns were in the returned result set. * This will be the number of columns in the CList widget. */ cols = mysql_num_fields(result_set); /* Create a new CList widget and make it accessible from * frm_table_display. */ clist_table = gtk_clist_new (cols); gtk_object_set_data_full(GTK_OBJECT(frm_table_display), "clist_table", clist_table, 0L); gtk_widget_show (clist_table); gtk_container_add (GTK_CONTAINER (scrolledwindow1), clist_table); gtk_clist_column_titles_show (GTK_CLIST (clist_table)); /* Now iterate through the columns, setting the column header * label for each column. */ for (counter = 0; counter < cols; counter++) { mysql_field_seek(result_set, counter); field = mysql_fetch_field(result_set); label = gtk_label_new (field->name); gtk_widget_show (label); gtk_clist_set_column_widget (GTK_CLIST (clist_table), counter, label); gtk_clist_set_column_width (GTK_CLIST (clist_table), counter, 80); } /* Next iterate through the rows. With each row, add a row * to the CList widget clist_table. */ while ((db_row = mysql_fetch_row (result_set)) != 0L) { for (counter = 0; counter < cols; counter++) { row[counter] = db_row[counter]; } gtk_clist_append(GTK_CLIST(clist_table), row); } mysql_close(conx); /* Send back the scrolledwindow widget that contains * the CList widget. */ return scrolledwindow1; }
only for RuBoard - do not distribute or recompile | |