Re: [gnome-db] Please Check this example



On 3/28/06, Daniel Espinosa <esodan gmail com> wrote:
> After following the steps to connect and access the data to create a simple
> example, I make this code, but the GNOME-DB widgets doesn't show the data; I
> test the GdaDataModel, accessing the values and it is valid, then may be is
> a bug on the Widgets.

See the attached file.

>
>  Please note that, I use GValue's functions: g_value_transform, to convert
> the GdaValue (that it is a GValue) into a String (then the

Beware that the g_value_transform() function may change the value:
"Performing transformations between value types might incur precision
lossage". That's the reason I've added the gda_value_transform() which
is supposed to convert only if it can do so without any data loss.

Vivien
/*
 * 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 <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 ();
			
		/*
			Create 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();
			
			cnn = gda_client_open_connection(client,
										gnome_db_login_dialog_get_dsn(logindlg),
										gnome_db_login_dialog_get_username(logindlg),
										gnome_db_login_dialog_get_password(logindlg),
										GDA_CONNECTION_OPTIONS_DONT_SHARE,
										&error);
			//
			//
			//
			// You don't need to check if cnn is opened, because the returned cnn will be NULL if the connection could not be opened.
			// better to check if cnn is NULL then.
			//
			//
			//
			
			if( gda_connection_is_opened (cnn)) {
								
				GdaDict *dict;
				GdaQuery *query;
				GdaDataModel *data_model;
							
				
				dict = GDA_DICT (gda_dict_new());
				
				gda_dict_set_connection(dict, cnn);
				
				if (! gda_dict_load (dict, &error)) {
					GtkWidget *msgdlg;
					msgdlg = gtk_message_dialog_new(NULL,
													GTK_DIALOG_MODAL,
													GTK_MESSAGE_ERROR,
													GTK_BUTTONS_OK,
													"Couldn't Load the Data Base's Dictionary!");
					gtk_dialog_run(GTK_MESSAGE_DIALOG(msgdlg));
					gtk_widget_destroy(msgdlg);
						
				}
				
				//
				//
				//
				// Note: For a simple example as this one you don't need a dictionary
				//
				//
				//
				
				
				// Destroy the Login Dialog not used any more
				 
				gtk_widget_destroy(logindlg);
				
				/* 
					Now Set Get the data from the Data Base
				*/
					
				
				query = gda_query_new_from_sql(dict, "SELECT * FROM customers", &error);
				
				if(error)
					g_printf("Error: %s\n", error->message);
				//
				//
				//
				// Here query will _always_ be a GdaQuery, and it will execute your SQL code. the error argument is simply a way
				// of telling you of some "warnings" it found during the parsing of the SQL you gave it.
				//
				//
				//	
				if(GDA_IS_QUERY(query)) {
					
					GtkWidget *grid;
					GtkWidget *vbox;
					gchar *sql;
					GdaCommand *command;
					
					sql = gda_renderer_render_as_sql(GDA_RENDERER(query), NULL, 0, NULL);
					
					g_printf("COMMAND TEXT: %s\n", sql);
					g_printf("QUERY TEXT: %s\n");
					
					//sql = "SELECT * FROM custumers";
					
					command = gda_command_new(sql, GDA_COMMAND_TYPE_SQL,GDA_COMMAND_OPTION_STOP_ON_ERRORS);
					
					data_model = gda_connection_execute_single_command (cnn,
							    command,
							    NULL,
							    &error);
					//
					//
					//
					// testing if data_model is NULL is enough. If it's not NULL then you can be sure it is a GdaDataModel
					//
					//
					//	
					if(!GDA_IS_DATA_MODEL(data_model)) {
						
						g_printf("It isn't a DataModel!\n");
					}
					else {
						
						GdaValue *value;
						gint i, j;
						
						g_printf("It IS a GdaDataModel!\n");
						
						GValue *val = g_new0(GValue, 1);
						g_value_init(val, G_TYPE_STRING);
						
						g_printf("VALUES:\n");
						//
						//
						//
						// you can use gda_data_model_dump(data_model, stdout)
						// which does the same thing with a nice formatting
						//
						//
						//
						for( i = 0; i < gda_data_model_get_n_rows(data_model); i++) {							
							for( j = 0; j <gda_data_model_get_n_columns(data_model); j++) {
								
								value = gda_data_model_get_value_at(data_model, j, i);
								
								g_value_transform(value, val);
								
								g_printf(" %s | ", g_value_get_string(val));
							}
							g_printf("\n");
						}
					}
				
					/* 
					Now Set up the Widget to Show the Data
					*/
					grid = gnome_db_form_new(data_model);
					
					vbox = lookup_widget(window1, "vbox");
					
					gtk_box_pack_end_defaults(vbox, grid);
					
					//
					//
					//
					// You need to add a gtk_widget_show (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,
												"Couldn't Connect to the Data Base!");
				gtk_dialog_run(GTK_MESSAGE_DIALOG(msgdlg));
				gtk_widget_destroy(msgdlg);
				
			}
						
		}
		
	  
	
	  gnome_db_main_run (NULL, NULL);
	  return 0;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]