This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!


Gambas speeds database development

By Mark Alexander Bain on December 03, 2004 (8:00:00 AM)

Share    Print    Comments   

Gambas, designed by Beno�t Minisini, is a new graphical development environment for Linux that aims to improve database development times.

Gambas installation is straightforward -- simply download the tar file from the Web site and use tar xzvf to unpack the required directories and files, then follow the instructions in the README file.

Once the software is installed, any developers who have dipped into Windows will immediately recognize the look and feel -- it's very like Visual Basic, and that's not a coincidence. Gambas is by no means a Visual Basic clone for Linux, but it does take many of the better features to provide a true rapid development environment. It has the standard components -- a project window, a toolbox, etc. -- and it takes very little effort for even a brand new programmer to build a form.

Gambas development environment

You can move between form design and coding by clicking on a form in the project window or its associated class file (in which the code is stored). A second way is to double-click on an object on the form (such as a button) or on the form itself. Doing so opens the default function of the object. For instance, the default method of a form is Form_Load. It is here that we would place any code that should run as soon as the form is first opened. For instance, a method to load a set of data into a combo box would be placed here to ensure that it will be loaded immediately:

PUBLIC SUB Form_Open()

The subroutine to load the combo box could be as simple as:

PRIVATE SUB load_combo()
combobox1.Add("Home Address")
combobox1.Add("Office Address")
combobox1.Add("Postal Address")

As you enter the code the application informs you about the inputs that each method expects, and helps you by giving you the possible methods and properties of objects. It also automatically adds required words, such as the END statement. You can see your end result by clicking on the green triangle in the project window.

Grids are just as easy to handle, but have no Add method. Instead, the numbers of rows and columns have to be define, then the grid must be written to as if it were a multidimensional array. For instance, the following code sets the grid to be two columns wide by three high and writes data to the top left cell:

gridview1[0,0]="Hello World"

The number of rows and columns can set in Form_Load or they can be varied from within the code, as required. For instance, the code for a method to add an extra row to the grid and to write to the first column of the new row would be:

PRIVATE SUB update_grid (newData AS String)

The advantage of putting this code into a method is that you can call it from other subroutines. For example, you could load the grid with the contents of a text box by the click of a button:

PUBLIC SUB Button1_Click()

In the combo box above we've hardcoded the contents into the program, but this, of course, is not a good idea. It is much more effective to store such lists in a database. Why? Imagine if we have to add something to the list. If the list is in a central database it can be modified easily, so that when a user logs on, the new list is immediately available. If not, users have to wait for you to recompile a new version of your software.

Gambas supports MySQL and PostgreSQL. Data loads need to take place in two stages -- first, the creation of the data structure, and second, the loading of any default data.

The first job is to create the database. You can log onto the default database as root and create the new database, but instead I recommend creating a file (e.g. schema.txt) containing the correct commands. This approach provides a record of what you've done. The following example is suitable for MySQL:

/*Start of file*/
drop database if exists customer_details;
/*Create the database*/
create database if not exists customer_details;
/*Create the data structure*/
create table if not exists customer_details.address_type (
id int auto_increment,
title varchar(255),
primary key (id)
/*Load default data*/
insert into customer_details.address_type (title) values ('Home Address');
insert into customer_details.address_type (title) values ('Office Address');
insert into customer_details.address_type (title) values ('Postal Address');
/*Create user accounts*/
GRANT select,insert,delete,update ON customer_details.*
TO bainm@localhost IDENTIFIED BY 'mypassword';
/*End of file*/

Assuming that you're in the right directory, you can now create the database by typing the following in a console window:

cat schema.txt | mysql -uroot -p<the right password> mysql

With the database in place you can read from it and write to it through the form as soon as you add the components that will allow the application to communicate with a database. To do this, go to the project window and click on the Project menu and then Properties. Go to the Components tab, tick gb.db, and press OK.

You now need to connect to the database from within the code. To do that, you need a global variable that will represent the actual connection object, and a function to set up the connection. You'll also need to rewrite Load_Combo to obtain its data from the database.

PRIVATE conn AS NEW Connection
PUBLIC SUB Form_Open()
IF make_connection()=TRUE THEN
PRIVATE FUNCTION make_connection() AS Boolean
WITH conn
.Type = "mysql"
.Host = "localhost"
.Login = "bainm"
.Password = "mypassword"
.Name = "customer_details"
TRY conn.Open
Message ("Cannot Open Database. Error = " & Error.Text)
PRIVATE SUB load_combo()
DIM res AS Result
DIM sql AS String
sql="select title from address_type"
res= conn.Exec(sql)
ComboBox1.Add (res!title)

In the code above we created a connection object at a global level that is available to all of the methods. Form_Load runs a function that tries to connect to the database and returns true or false according to whether or not it was successful. Load_combo now runs a query using a SQL statement on the database, returning a set of results that can be loaded into the combo box.

In the example above the user details are hardcoded, but this is not necessarily a good idea. Instead it might be better to request these from the user through a second form. It really depends on the levels of security that you require.

Writing to the the database is as easy as reading from it. Simply build either an update or insert SQL statement and send it to the Exec method of the connection. For example, a subroutine to add an extra item to the list could be:

PRIVATE SUB addToList (newItem AS String)
DIM res AS Result
DIM sql AS String
sql="insert into address_type (title) values ('" & newItem & "')"

Equally easy is a subroutine to change the text of an item in the list:

PRIVATE SUB changeListItem (newText AS String,oldText AS String)
DIM res AS Result
DIM sql AS String
sql="update address_type set title='" & newText & "' where title ='" & oldText & "'"

As with the update_grid method, these subroutines can be used in conjunction with buttons and textboxes to make them completely interactive.

Gambas still has a number of areas in which it needs to be developed. For instance, it is not yet multiplatform -- it works only on Linux at the moment. However, the simplicity of its GUI development and database access are both impressive. It is worth considering Gambas the next time that you need a new application.

Share    Print    Comments   


on Gambas speeds database development

Note: Comments are owned by the poster. We are not responsible for their content.


Posted by: Anonymous Coward on December 04, 2004 12:29 AM
open-source VB? 0_o



Posted by: Anonymous Coward on December 05, 2004 10:15 AM
I can't switch completely to Linux until there's something like VB available. When I say 'like', preferably that would be something which can compile VB projects without modification. (I wish!)

I've been writing in VB since the early 90's, I've developed dozens of apps and there are two recurring themes in the customer feedback I get:

1) they're STABLE. They don't crash.
2) they're user-friendly. (This one's more me than the language)

VB has an underserved reputation as a 'bad' language. I think that comes about because any charlie can pick up a copy, make a form with a couple of buttons and call themselves a programmer. I've found it to be very, very stable, provided you avoid undocumented hacks and weird API calls.

I've taken a look at but I have megs and megs of source code, and right now I can take my default routines and tools for VB and build a utility in no time. It's a second language to me, I'm comfortable with it and I have no intention of learning another.


PS I don't think Gambas is open source.



Posted by: Anonymous Coward on December 05, 2004 10:20 AM
"PS I don't think Gambas is open source."

My apologies, I just checked and it is.




Posted by: Anonymous Coward on December 06, 2004 01:50 AM
"I'm comfortable with it and I have no intention of learning another."

All programmers should keep their skills up to date and seek to improve their knowledge by learning new languages. Joel on Software suggests (I think) one new language a year. You know nothing if all you have learned is from VB, it's a dumbed-down environment. I can say that because I spent a lot of time with the language. I thought I knew what I was doing until I tried to learn a new platform and was exposed to a lot of concepts I didn't know. In turn, that made me more knowledgeable about the VB work I had to do.

That you think VB is stable is laughable. I've admin'ed several well-written VB apps that crash without cause. The platform is itself is not stable. Perhaps if you ventured out from your cosy, deprecated VB world you might discover something better. Instead, you've choosen to defend the indefensable while admitting ignorance of any alternatives.


Re: wth?

Posted by: Anonymous [ip:] on February 24, 2008 10:33 PM
if you want to learn GAMBAS language, a new website in french and english is born :


Do we need basic?

Posted by: Anonymous Coward on December 04, 2004 07:07 AM
Isn't the good things about VB the interface? Why use the language?


Re:Do we need basic?

Posted by: Anonymous Coward on December 04, 2004 10:00 AM
The language is easy for beginners.


Re:Do we need basic?

Posted by: Anonymous Coward on December 04, 2004 11:47 AM
And a pain in the ass for professionals<nobr> <wbr></nobr>:(


Re:Do we need basic?

Posted by: Anonymous Coward on December 04, 2004 01:42 PM
Professionals have plenty of stuff on linux to develop in and on already. Gambas makes making those small simple programs easy. Its great for teaching programming concepts in too.


Re:Do we need basic?

Posted by: flacco on December 04, 2004 09:00 PM
easier than python?


Re:Do we need basic?

Posted by: Anonymous Coward on December 07, 2004 02:59 AM
We need BASIC because everyone knows that it isn't a real OS without one. Heck even MS was forced to include qbasic in Win95 so it could be accepted as a real OS.

Just kidding - but really, if someone wants to create a BASIC for Linux why not? If you don't want to use it, then use a different scripting langague (hmmmm, isn't this what we say about Linux GUI Desktops, Linux Window Managers, Linux Printing, etc.?).


what about ooo 2.0 base?

Posted by: Anonymous Coward on December 04, 2004 03:19 PM
what about open office base, included in ooo 1.9.x snapshots and available in 2.0? It's almost the same as MS Access, and better integrated with rest of ooo than gambas.


Re:what about ooo 2.0 base?

Posted by: Anonymous Coward on December 04, 2004 05:31 PM
err.. Access is a form on top of a database. Gambas is about writing applications (which in this example happen to be a form on top of a database)...


Re:what about ooo 2.0 base?

Posted by: Anonymous Coward on January 31, 2005 11:48 AM
Gambas has been in development for about 3 years, at least a year and a half longer than the OpenOffice guys even admitted an Access-like app might be a good idea. Gambas is also a bit more general-purpose than Access or OpenOffice Base.


Benoit you Rule

Posted by: Anonymous Coward on December 04, 2004 05:07 PM
Thanks for Gambas


Not a problem

Posted by: Anonymous Coward on December 05, 2004 12:58 AM
>>Gambas still has a number of areas in which it needs to be developed. For instance, it is not yet multiplatform

Who cares. Let other OSes worry about the RDE's (windows already has) we will take care of our own. Gambas is a good reason to SWITCH to using Linux.




Posted by: Anonymous Coward on December 09, 2004 02:16 PM
As a VB developer who's fed up with M$hit this is paradise. I've learned all the major languages and can honestly say that VB is great for RAD but to be able to do that on Linux is even better. This also comes as I'm downloading Mandrake 10.1 to switch my laptop to pure linux after several windows corruption related failures. Thanks bunches. TSL



Posted by: Anonymous Coward on January 16, 2005 02:23 PM
When Gambas can serialize objects and persist them to a file, I will use it. Until then, it is fun to play with but not quite ready for a full commitment.


Gambas caters for the lesser experienced

Posted by: Anonymous [ip:] on December 13, 2007 04:26 PM
This is great news for Linux adoption. There will always be a lot of developers who don't do the job full-time or are only part way into a learning curve. They can now move to Linux and will find the whole experience very refreshing, PLUS they'll have opportunity to check out all of the other languages for free (and on a stable platform).

My suggestion to people is, when learning a new language, always have something real to create :
A) Because it makes you learn the language more thoroughly
B) Your efforts may pay dividends, rather than being a "look at what I did"

Imagine, you've created this really useful utility and your boss says "we could use that". Sure boss, let me just spend an hour building up a new Unix server for you and it's yours ;-)


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya