The First Steps in GDA and GNOME-DB
Firts all let me say that GDA is a library where all the interfaces to the Data Bases are, and Gnome-Db is the library that let you use specialy designed widget to view and edit data, using a GUI program.
GDA arquitecture
GDA (Gnome Data Access), is a set of objects that abstracts the way you read and write data to a Data Base; it has Models for each kind of data you reads and has an API for each one.
Connection and Retrieve Data
Basics
Follow the next basic steps to access data in a Data Base using GDA:
- Create a Client.
- Open a Connection, using the Client, the DNS, username and password. The DNS could be created using the GDA utilities and/or using the GUI provider by GNOME-DB. The GNOME-DB privides an example DNS and Data Base using the SQLite enbeded in GDA; we'll use this one for our example.
If the Connection is opened, then you can create a Command (GdaCommand), like an SQL.
- Execute the command created using the Connection object; if you execute a valid SQL sentence, you'll get back a Data Model, wich has the data you can use and/or update.
Access the Data using the Gda's API to access values in the DataModel, and/or set the Data Model to a Gnome-Db widget to show it.
Hellow World in GDA
The following example used Anjuta to create an standar GNOME project, and then modify the main() function to allow a simple Window setup; however, it is better to call a function out of main() to setup it.
#include <gnome.h>
#include "interface.h"
#include "support.h"
#include <libgnomedb/libgnomedb.h>
int
main (int argc, char *argv[])
{
GtkWidget *window1;
GtkWidget *logindlg;
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
gnome_db_init (PACKAGE, VERSION,
argc, argv);
/*
* Create the Main window
*/
window1 = create_window1 ();
/*
SetUp the Widget to Show the Data:
First: Connect to the Server
*/
logindlg = gnome_db_login_dialog_new("Select the Data Base Provider to Login");
if (gnome_db_login_dialog_run(GNOME_DB_LOGIN_DIALOG(logindlg)))
{
GdaClient *client;
GdaConnection *cnn;
GError *error = NULL;
client = gda_client_new();
// Open the Connection to the Data Base, using the DSN, username and password given by the GnomeDbLoginDialog
cnn = gda_client_open_connection(client,
gnome_db_login_dialog_get_dsn(GNOME_DB_LOGIN_DIALOG(logindlg)),
gnome_db_login_dialog_get_username(GNOME_DB_LOGIN_DIALOG(logindlg)),
gnome_db_login_dialog_get_password(GNOME_DB_LOGIN_DIALOG(logindlg)),
GDA_CONNECTION_OPTIONS_DONT_SHARE,
&error);
// Destroy the Login Dialog not used any more
gtk_widget_destroy(logindlg);
// Verify the connection
if( gda_connection_is_opened (cnn)) {
GdaDataModel *data_model;
GtkWidget *grid;
GtkWidget *vbox;
gchar *sql;
GdaCommand *command;
/*
Now Set the SQL statement to get the data from the Data Base
*/
sql = "SELECT * FROM customers";
command = gda_command_new(sql, GDA_COMMAND_TYPE_SQL,GDA_COMMAND_OPTION_STOP_ON_ERRORS);
// Execute the Command to get a DataModel
data_model = gda_connection_execute_single_command (cnn,
command,
NULL,
&error);
if(GDA_IS_DATA_MODEL(data_model)) {
/*
Now Set up the Widget to Show the Data
*/
grid = gnome_db_grid_new(data_model);
gtk_widget_show(grid);
vbox = lookup_widget(window1, "vbox");
gtk_box_pack_end_defaults(GTK_BOX(vbox), grid);
/* Show the Main window*/
gtk_widget_show (window1);
}
else {
GtkWidget *msgdlg;
msgdlg = gtk_message_dialog_new(NULL,
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"I can't get the Data from to the Data Base!\n\
may be the SQL sentences is wrong");
gtk_dialog_run(GTK_DIALOG(msgdlg));
gtk_widget_destroy(msgdlg);
}
}
else {
GtkWidget *msgdlg;
msgdlg = gtk_message_dialog_new(NULL,
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"Couldn't Connect to the Data Base!");
gtk_dialog_run(GTK_DIALOG(msgdlg));
gtk_widget_destroy(msgdlg);
}
}
gnome_db_main_run (NULL, NULL);
return 0;
}As you can see, we use the gnome_db_init() enstead the gnome_app_init() for Gnome-Db and GDA initiation; at the end you'll find the gnome_db_main_run(), to manage the events in the MainWindow once it is showed in the screen.
It is important that you add a gnome_db_main_quit() function when you destroy the MainWindow or select the Exit option in the File menu.