You are here
MySQL Vala Program Example
Summary: In this article we have a short look at a simple MySQL example program written in Vala.
Recently a customer pointed me to a programming language called Vala. Vala is a C-style programming language generating C code which afterwards can be compiled and linked with the normal gcc
.
This I found pretty useful to not mess around with pointers and all this stuff in C and to be capable anyway to write C programs for some projects I had in mind in my head since long.
Vala is mostly used around the Gnome Project.
Vala
Vala declares itself as:
Vala is a new programming language that allows modern programming techniques to be used to write applications ... Before Vala, the only ways to program for the platform were with the machine native C API, which exposes a lot of often unwanted detail, with a high level language that has an attendant virtual machine, such as Python or the Mono C# language, or alternatively, with C++ through a wrapper library.
Vala is different from all these other techniques, as it outputs C code which can be compiled to run with no extra library support beyond the GNOME platform. This has several consequences, but most importantly:
- Programs written in Vala should have broadly similar performance to those written directly in C, whilst being easier and faster to write and maintain.
- A Vala application can do nothing that a C equivalent cannot. Whilst Vala introduces a lot of language features that are not available in C, these are all mapped to C constructs, although they are often ones that are difficult or too time consuming to write directly.
- As such, whilst Vala is a modern language with all of the features you would expect, it gains its power from an existing platform, and must in some ways comply with the rules set down by it.
MySQL Example
After playing around with some simple programs like the hello.vala I wanted to write my own little MySQL program in Vala. But I did not find one single complete runnable example for MySQL. :-(
Some Vala documentation about MySQL is available here.
After searching around for a while and loosing a lot of time I finally got it myself. I hope the following MySQL Program Example in Vala helps you to get a faster start with this nice language and MySQL:
using Mysql; int main (string[] args) { int rc = 0; ClientFlag cflag = 0; string host = "127.0.0.1"; string user = "root"; string password = ""; string database = "test"; int port = 3306; string socket = null; Database mysql = new Mysql.Database (); var isConnected = mysql.real_connect(host, user, password, database, port, socket, cflag); if ( ! isConnected ) { rc = 1; stdout.printf("ERROR %u: Connection failed: %s\n", mysql.errno(), mysql.error()); return rc; } stdout.printf("Connected to MySQL server version: %s (%lu)\n" , mysql.get_server_info() , (ulong) mysql.get_server_version()); string sql = "SELECT * FROM test LIMIT 10"; rc = mysql.query(sql); if ( rc != 0 ) { stdout.printf("ERROR %u: Query failed: %s\n", mysql.errno(), mysql.error()); return rc; } Result ResultSet = mysql.use_result(); string[] MyRow; while ( (MyRow = ResultSet.fetch_row()) != null ) { stdout.printf("id: %s | data: %s | ts: %s\n", MyRow[0], MyRow[1], MyRow[2]); } // free_result is called automatically // mysql_close is called automatically return rc; }
This code will only run with Vala v0.12 and newer.
How to install Vala
The following installation instruction [ 2 ] worked for me:
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 7DAAC99C sudo add-apt-repository ppa:vala-team sudo apt-get update sudo apt-get install valac vala-utils vala-doc valac-dbg valac --version sudo apt-get install libgee-dev sudo apt-get install gedit-vala-plugin vala-gen-project sudo apt-get install valide
Compile the MySQL Vala example program
I used the following command to compile my MySQL Vala Program Example:
valac --pkg=mysql --Xcc=-lmysqlclient mysql_ex1.vala --Xcc=-I/home/mysql/src/mysql-5.1.55 \ --Xcc=-L/home/mysql/product/mysql-5.1.55/lib -v
If you want too see the C-code which is generated you have to add the --ccode
option.
The generated binary file in my case is just 13876 bytes long.
And now have fun playing around with Vala!
- Shinguz's blog
- Log in or register to post comments