Home > Articles > Data > MySQL

Using GTK+ to Build GUI MySQL Applications—A Tabular Report

Matt Stucky, author of MySQL: Designing User Interfaces, shows you how to use two very popular GPL tools to create Linux database applications the way you may be used to with SQL Server and Visual Basic. That is, there is a "backend database," MySQL in this case, and an "Integrated Development Environment" (IDE) that creates the User Interface (UI). Finally, the programmer must write code to connect to be executed when an event is triggered.
This article is excerpted from MySQL: Designing User Interfaces, by Matt Stucky.
Like this article? We recommend

Like this article? We recommend

Using GTK+ to Build GUI MySQL Applications—A Tabular Report

This report in Listing 1 is a simple tabular report that, for lack of a better term, will be called a "data display control." It is called a "data display control" because it queries a MySQL database table and displays the resulting rows in tabular format. It is read-only as presented here; the user cannot make changes and write them to the database in this application.

Be aware that although they are not covered here, several resources went into the creation of this application:

  • Glade, the GTK+ GUI builder, is an IDE to allow the developer to "point and click" the UI into form in a graphical manner. This follows along the same basic lines as VB or VC++.

  • The application uses the MySQL system tables, which requires that MySQL be installed. MySQL is a GPL database that is very popular in the Linux community. It is small, fast, and robust. It operates along the same lines as SQL Server or other databases in that it runs a daemon (or "service" in the Win32 world) that processes and responds to incoming requests for data.

  • Also note that GTK+ uses "widgets" instead of the term "controls" that you may be familiar with. Their function is the same.

Listing 1 ddc.c for the tabular report

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <mysql.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
GtkWidget *create_ddc (gchar *table_name, gchar *server_name,
            gchar *user_name, gchar *user_pwd,
            gchar *db_name, GtkWidget *frm_target)
{
 MYSQL   *conx;
 GtkWidget *scrolledwindow1;
 GtkWidget *clist_table;
 GtkWidget *label;
 gint   counter;
 gint   cols = 3;
 gchar   *sql;
 MYSQL_RES *result_set;
 MYSQL_ROW db_row;
 MYSQL_FIELD *field;
 gchar   *row[20] = {"", "", "",
"", "", 
            "", "", "", "",
"",
            "", "", "", "",
"",
            "", "", "", "",
""};
 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, server_name, user_name, 
           user_pwd, db_name, 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);
 cols = mysql_num_fields (result_set);
 clist_table = gtk_clist_new (cols);
 gtk_object_set_data_full(GTK_OBJECT(frm_target),
"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));
 /* First iterate through the columns. */
 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. */
 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);
 return scrolledwindow1;
}

Listing 2 contains the callbacks for the tabular form. Listings 1 and 2 are the most important parts of the "tabular" report presented here.

Listing 2 callbacks.c for the tabular report

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include <mysql.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "ddc.h"
GtkWidget *frm_tabular;
void
on_frm_tabular_show          (GtkWidget    *widget,
                    gpointer     user_data)
{
 gchar   *sql;
 MYSQL   *conx;
 GtkWidget *scrolledwindow1;
 GtkWidget *statusbar1;
 /* When frm_tabular opens, it needs to show the
top
  * salespeople in descending order of commissions paid.
  */
 g_print("on_frm_tabular_show.\n");
 /* First, connect to the database. */
 conx = mysql_init((MYSQL *)0L);
 conx = mysql_real_connect(conx, "localhost", 0L,
              0L, "mysql", 0, 0L, 0);
 if (conx == 0L) 
   {
     g_print("Unable to connect to database.\n");
     gtk_statusbar_push(GTK_STATUSBAR(lookup_widget(frm_tabular,
           "statusbar1")), 1, "Unable to Connect to
database.");
     return;
   }
 g_print("connected to mysql db.\n");
 mysql_close (conx);
 g_print("First connection closed.\n");
 sql = " db";
 gtk_widget_destroy(GTK_WIDGET(lookup_widget(frm_tabular,
        "scrolledwindow1")));
 g_print("Calling create_ddc.\n");
 scrolledwindow1 = create_ddc(sql, "localhost", 0L, 
                0L, "mysql", frm_tabular);
 g_print("Returned from create_ddc.\n");
 
 gtk_widget_ref(scrolledwindow1);
 gtk_object_set_data_full(GTK_OBJECT(frm_tabular),
        "scrolledwindow1", scrolledwindow1,
        0L);
 gtk_box_pack_start(GTK_BOX(lookup_widget(frm_tabular,
        "vbox1")), scrolledwindow1, TRUE, TRUE, 0);
 gtk_widget_show(scrolledwindow1);
 /* Unfortunately, the packing box widgets don't have
any
  * way to insert a child widget at a certain position;
it
  * can be inserted only at the start and end. Therefore,
  * destroy the statusbar widget
  * created in Glade and create a new one.
  *
  * Remember, however, that the statusbar was needed
prior
  * to this point to communicate with the user.
  */
 gtk_widget_destroy(GTK_WIDGET(lookup_widget(frm_tabular,
     "statusbar1")));
 statusbar1 = gtk_statusbar_new();
 gtk_box_pack_start(GTK_BOX(lookup_widget(frm_tabular,
        "vbox1")), statusbar1, FALSE, FALSE, 0);
 gtk_widget_show(statusbar1);
 gtk_statusbar_push(GTK_STATUSBAR(lookup_widget(frm_tabular,
           "statusbar1")), 1, "Done.");
}
gboolean
on_frm_tabular_delete_event      (GtkWidget    *widget,
                    GdkEvent    *event,
                    gpointer     user_data)
{
 g_print("on_frm_delete_event.\n");
 
 gtk_main_quit();
 return FALSE;
}

Figure 1 shows the finished tabular report running in its own process space.

Figure 1 The tabular report is a very rough "grid control."

Listing 3 is the Glade-generated file for the tabular executable. This is the output from Glade that will be compiled to create the main form of the application; the create_frm_tabular() function is the only one in the file interface.c (in this case). Note that interface.c is only one of the files output by Glade; the rest are presented in later listings.

Listing 3 interface.c for the tabular executable

/ * DO NOT EDIT THIS FILE - it is generated by Glade.
 */
#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 "callbacks.h"
#include "interface.h"
#include "support.h"
GtkWidget*
create_frm_tabular (void)
{
 GtkWidget *frm_tabular;
 GtkWidget *vbox1;
 GtkWidget *scrolledwindow1;
 GtkWidget *clist1;
 GtkWidget *label1;
 GtkWidget *label2;
 GtkWidget *label3;
 GtkWidget *statusbar1;
 frm_tabular = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_object_set_data (GTK_OBJECT (frm_tabular), "frm_tabular",
frm_tabular);
 gtk_window_set_title (GTK_WINDOW (frm_tabular), "Top Commission
Earners");
 gtk_window_set_default_size (GTK_WINDOW (frm_tabular), 280,
500);
 vbox1 = gtk_vbox_new (FALSE, 0);
 gtk_widget_ref (vbox1);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular), "vbox1",
vbox1,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (vbox1);
 gtk_container_add (GTK_CONTAINER (frm_tabular), vbox1);
 scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
 gtk_widget_ref (scrolledwindow1);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular),
"scrolledwindow1", scrolledwindow1,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (scrolledwindow1);
 gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE,
0);
 clist1 = gtk_clist_new (3);
 gtk_widget_ref (clist1);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular), "clist1",
clist1,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (clist1);
 gtk_container_add (GTK_CONTAINER (scrolledwindow1), clist1);
 gtk_clist_set_column_width (GTK_CLIST (clist1), 0, 80);
 gtk_clist_set_column_width (GTK_CLIST (clist1), 1, 80);
 gtk_clist_set_column_width (GTK_CLIST (clist1), 2, 80);
 gtk_clist_column_titles_show (GTK_CLIST (clist1));
 label1 = gtk_label_new ("label1");
 gtk_widget_ref (label1);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular), "label1",
label1,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (label1);
 gtk_clist_set_column_widget (GTK_CLIST (clist1), 0, label1);
 label2 = gtk_label_new ("label2");
 gtk_widget_ref (label2);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular), "label2",
label2,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (label2);
 gtk_clist_set_column_widget (GTK_CLIST (clist1), 1, label2);
 label3 = gtk_label_new ("label3");
 gtk_widget_ref (label3);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular), "label3",
label3,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (label3);
 gtk_clist_set_column_widget (GTK_CLIST (clist1), 2, label3);
 statusbar1 = gtk_statusbar_new ();
 gtk_widget_ref (statusbar1);
 gtk_object_set_data_full (GTK_OBJECT (frm_tabular),
"statusbar1", statusbar1,
              (GtkDestroyNotify) gtk_widget_unref);
 gtk_widget_show (statusbar1);
 gtk_box_pack_start (GTK_BOX (vbox1), statusbar1, FALSE, FALSE,
0);
 gtk_signal_connect (GTK_OBJECT (frm_tabular), "show",
           GTK_SIGNAL_FUNC (on_frm_tabular_show),
           NULL);
 gtk_signal_connect (GTK_OBJECT (frm_tabular),
"delete_event",
           GTK_SIGNAL_FUNC (on_frm_tabular_delete_event),
           NULL);
 return frm_tabular;
}

Listing 4 is the support.c file, again as output by Glade. Glade generates a larger file with other utility functions, but this file has been reduced to a minimum to present only the function necessary to this demonstration. The lookup_widget() function is required to find a "child" widget (such as a listbox or command button) inside a "parent" widget (such as a window or "form").

Listing 4 support.c

/* DO NOT EDIT THIS FILE - it is generated by Glade.
 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtk.h>
#include "support.h"
GtkWidget*
lookup_widget             (GtkWidget    *widget,
                    const gchar   *widget_name)
{
 GtkWidget *parent, *found_widget;
 for (;;)
  {
   if (GTK_IS_MENU (widget))
    parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
   else
    parent = widget->parent;
   if (parent == NULL)
    break;
   widget = parent;
  }
 found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT
(widget),
                          widget_name);
 if (!found_widget)
  g_warning ("Widget not found: %s", widget_name);
 return found_widget;
}

Listing 5 is the main.c file for this demonstration. It is the starting point for the code. Note that it essentially has one function: to call create_frm_tabular() and display the resulting widget, which in this case happens to be a window. Because only one window widget was created in Glade, Glade automatically added that window to main(), hence the comments about "having something to show."

Listing 5 main.c

/*Initial main.c file generated by Glade. Edit as
required.
 * Glade will not overwrite this file.
 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "interface.h"
#include "support.h"
GtkWidget *frm_tabular;
int
main (int argc, char *argv[])
{
 gtk_set_locale ();
 gtk_init (&argc, &argv);
 /*The following code was added by Glade to create one of each

  * component (except popup menus), just so you see something

  * after building the project. Delete any components you
don't
  * want shown initially. 
  */
 frm_tabular = create_frm_tabular ();
 gtk_widget_show (frm_tabular);
 gtk_main ();
 return 0;
}

Listing 6 is the command needed to compile the application. It will of course produce an executable with the name "a.out" by default. The quotes around the gtk-config... are actually back-tick characters; the back-tick character normally shares the key with the tilde (~) character and is generally located at the top left of the keyboard. The -I is the directory for include files; the -L is the location of the library files specified by the -l options. gcc is the GNU C compiler that comes standard with every Linux distribution (you may not have installed it, however). The –Wall flag tells gcc to output "all warnings."

Listing 6 The command line to compile the application

gcc -Wall *.c ´gtk-config --cflags --libs´ -I/usr/include/mysql

-L/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz

Figure 2 shows the same control, displaying data from a different database on the author's system; the data shown in Figure 2 is from one of the examples in MySQL: Building User Interfaces (see the end of this article for more information).

Figure 2 The same "data display control," displaying salesperson ranking data.

My book, MySQL: Building User Interfaces, is an attempt to show that developers with GUI toolkit and database experience can make the jump to MySQL, GTK+, and Glade with minimal effort. (Most of my experience is with VB and SQL Server.)

Additionally, experienced developers will appreciate two things I cover in the book that are not demonstrated in this article:

  • First, how to use the same source code to compile for either Linux or Windows.

  • Second, using XML and glade.h to build the user interface at run time rather than compile time, still using C.

I hope you have found this article informative and that you find my book equally so.

About This Article

This article is excerpted from MySQL: Designing User Interfaces by Matt Stucky (New Riders Publishing, 2001, ISBN 073571049X). Refer to Chapter 12, "Management Reporting Construction," for more detailed information on the material covered in this article.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020