Connect to database
With GNATColl library is possible to work with PostgreSQL and Sqlite (embedded database).
Checking if everything is in place for database development
If you successfully install AdaCore and compile GNATColl library, you can start writing and checking if everything works as expected.
Create new project, project file must contain gnatcoll reference:
My dbsample1.gpr file contains:
with "gnatcoll"; with "gnatcoll_postgres"; project Dbsample1 is for Source_Dirs use ("src"); for Object_Dir use "obj"; for Main use ("main.adb"); end Dbsample1;
And main.adb file:
with gnatcoll.SQL.Postgres; use gnatcoll.SQL.Postgres; with gnatcoll.SQL.Exec; use gnatcoll.SQL.Exec; with Ada.Text_IO; use Ada.Text_IO; procedure Main is DB_Descr : Database_Description; DB : Database_Connection; IsOpen : Boolean; begin DB_Descr := Setup(Database => "appdb", User => "postgres", Host => "localhost", Password => "postgres", Port => 5432 ); DB := DB_Descr.Build_Connection; IsOpen := DB.Check_Connection; if IsOpen then Put_Line("Connection is open."); else Put_Line("Last Db error = " & DB.Error); end if; -- reset state of connection for reuse Reset_Connection(DB); Free (DB); Free (DB_Descr); end Main;
You do not forget to add PostgreSQL/bin folder to local windows PATH, or copy postgres libraries to executing application folder.
If you do everything as needed, main.exe will be created and when you run it, you will get something minimal but without any errors :
H:\Ada\SOURCE\DBSample1\obj\main Connection is open. [2016-07-23 22:38:22] process terminated successfully, elapsed time: 00.30s