wiki:SQL

Version 6 (modified by vadim.godunko, 10 years ago) ( diff )

--

This module will be available in Matreshka 0.0.7

SQL Database Access

SQL Database Access module provides simple generic API to access to SQL databases. Currently supported databases:

API contains two packages. The SQL.Databases package provides SQL_Database type to connect to database server and manage transactions, and the SQL.Queries package provides SQL_Query type to prepare, bind, execute queries and retrieve results.

Example

This example creates database, creates table in the database, fills it and retrieve values from it.

with Ada.Wide_Wide_Text_IO;

with League.Strings;
with League.Values.Strings;
with SQL.Databases;
with SQL.Queries;

procedure Example is

   function "+"
    (Item : Wide_Wide_String) return League.Strings.Universal_String
       renames League.Strings.To_Universal_String;

   DB_Type : constant League.Strings.Universal_String := +"SQLITE3";
   DB_Name : constant League.Strings.Universal_String := +"test.db";

   D : aliased SQL.Databases.SQL_Database
     := SQL.Databases.Create (DB_Type, DB_Name);

begin
   D.Open;

   --  Create table

   declare
      Q : SQL.Queries.SQL_Query := SQL.Queries.Create (D);

   begin
      Q.Prepare
       (+"CREATE TABLE point (x CHARACTER VARYING, y CHARACTER VARYING)");
      Q.Execute;
   end;

   --  Fill data

   declare
      Q : SQL.Queries.SQL_Query := SQL.Queries.Create (D);

   begin
      Q.Prepare (+"INSERT INTO point (x, y) VALUES (:x, :y)");
      Q.Bind_Value (+":x", League.Values.Strings.To_Value (+"abc"));
      Q.Bind_Value (+":y", League.Values.Strings.To_Value (+"xyz"));
      Q.Execute;
   end;

   --  Retrieve data from table

   declare
      Q : aliased SQL.Queries.SQL_Query := SQL.Queries.Create (D);

   begin
      Q.Prepare (+"SELECT x, y FROM point");
      Q.Execute;

      while Q.Next loop
         Ada.Wide_Wide_Text_IO.Put_Line
          (League.Values.Strings.To_Universal_String
            (Q.Value (1)).To_Wide_Wide_String
             & ":"
             & League.Values.Strings.To_Universal_String
                (Q.Value (2)).To_Wide_Wide_String);
      end loop;
   end;
end Example;
Note: See TracWiki for help on using the wiki.