viernes, 3 de abril de 2015

Ejemplo conexion Vala con PostgreSql

/**
 * To compile:
 *  valac --pkg libpq test.vala -X -lpq
 */

using GLib;
using Postgres;

public static int main (string[] args)
{
        string _url_servidor = "hostaddr=192.168.1.10 ";
        string _port_DB = "port=5432 ";
        string _usuario_DB = "user=your user  ";
        string _passwrd_user_DB = "password=your password ";
        string _nombredb;

        string connectionString = _url_servidor+_port_DB+_usuario_DB+_passwrd_user_DB;

        if (args.length > 1) {
                _nombredb = args[1];
        }else{
                  _nombredb = "dbname = osirisdb ";
        }

    /* Make a connection to the database */
        Database conn = Postgres.connect_db (connectionString+_nombredb);

        /* Check to see that the backend connection was successfully made */
        if (conn.get_status () != ConnectionStatus.OK) {
                stderr.printf ("Connection to database failed: %s", conn.get_error_message ());
                return 1;
        }else{
                  stderr.printf ("Connection to database OK..\n");
        }


    return 0;
}

No hay comentarios: