From: azdakiel@gmail.com
Subject: Gnade/ODBC example - please help
Date: Fri, 1 Aug 2008 05:18:38 -0700 (PDT)
Date: 2008-08-01T05:18:38-07:00 [thread overview]
Message-ID: <4962abea-7622-45b3-a121-49286c636421@2g2000hsn.googlegroups.com> (raw)
Hi,
I'm new here. I also just started my advanture with ADA.
I'm building a portal using AWS (my project for university) and i
need to connect to a MySQL database. I using Gnade/ODBC interface.
There is simple example, in the Gnade User's Guide, how to connect
and get some data from databese. And it works.
And now my problem: This example shows how to pass integer parameter
to the query and get string and float from the database.
I trying to pass to the query string and the boolean (...WHERE NAME =
name AND IsVisible = visible...) and I can't. I have no idea how to
rewrite this example.
Is there anyone who can tell me how to change this example?
---------------=========The Example========------------
with Ada.Characters.Handling;
with Ada.Command_Line;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;
with GNU.DB.SQLCLI; use GNU.DB.SQLCLI;
with GNU.DB.SQLCLI.Bind;
with GNU.DB.SQLCLI.Info; use GNU.DB.SQLCLI.Info;
with GNU.DB.SQLCLI.Info.Debug;
with GNU.DB.SQLCLI.Environment_Attribute;
use GNU.DB.SQLCLI.Environment_Attribute;
with GNU.DB.SQLCLI.Environment_Attribute.Debug;
with GNU.DB.SQLCLI.Connection_Attribute;
use GNU.DB.SQLCLI.Connection_Attribute;
with GNU.DB.SQLCLI.Connection_Attribute.Debug;
use GNU.DB.SQLCLI;
with GNAT.Traceback.Symbolic;
procedure odbc_mysql is
package RIO is new Ada.Text_IO.Float_IO (SQLDOUBLE);
EnvironmentHandle : SQLHENV;
ConnectionHandle : SQLHDBC;
ServerName : constant String := String'("test");
UserName : constant String := String'("test");
Authentication : constant String := String'("test");
Quoting_Character : Character := Character'Val (34);
function QuoteIdentifier (ID : String) return String;
procedure Get_Identifier_Info;
function QuoteIdentifier (ID : String) return String is
begin
return Quoting_Character & ID & Quoting_Character;
end QuoteIdentifier;
pragma Inline (QuoteIdentifier);
procedure Get_Identifier_Info is
QC : constant Driver_Info_String :=
Driver_Info_String (SQLGetInfo
(ConnectionHandle,
SQL_IDENTIFIER_QUOTE_CHAR));
begin
if QC.Value'Length /= 1 then
null;
else
Quoting_Character := QC.Value (QC.Value'First);
end if;
end Get_Identifier_Info;
begin
SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE,
EnvironmentHandle);
SQLSetEnvAttr (EnvironmentHandle,
Environment_Attribute_ODBC_Version'
(Attribute => SQL_ATTR_ODBC_VERSION,
Value => SQL_OV_ODBC3));
SQLAllocHandle (SQL_HANDLE_DBC, EnvironmentHandle,
ConnectionHandle);
SQLConnect (ConnectionHandle => ConnectionHandle,
ServerName => ServerName,
UserName => UserName,
Authentication => Authentication);
Get_Identifier_Info;
declare
package Double_Binding is new
GNU.DB.SQLCLI.FloatBinding (SQLDOUBLE);
package DB renames Double_Binding;
type ManagerID is new SQLINTEGER;
type ManagerID_Ptr is access all ManagerID;
package ManagerID_Binding is new
GNU.DB.SQLCLI.Bind (ManagerID, ManagerID_Ptr);
package MB renames ManagerID_Binding;
StatementHandle : SQLHSTMT;
Search_Manager : aliased ManagerID := 2;
Len : aliased SQLINTEGER := 0;
Name : aliased String := 20 * '.';
Firstname : aliased String := 20 * '.';
Len_Firstname : aliased SQLINTEGER;
Len_Name : aliased SQLINTEGER;
Len_Salary : aliased SQLINTEGER;
Salary : aliased SQLDOUBLE;
begin
SQLAllocHandle (SQL_HANDLE_STMT, ConnectionHandle,
StatementHandle);
SQLPrepare (StatementHandle,
"SELECT " & QuoteIdentifier ("NAME") & ", " &
QuoteIdentifier ("FIRSTNAME") & ", " &
QuoteIdentifier ("SALARY") &
" FROM " & QuoteIdentifier ("EMPLOYEES") & " " &
"WHERE " & QuoteIdentifier ("MANAGER") & " = ? "
&
"ORDER BY " & QuoteIdentifier ("NAME") & "," &
QuoteIdentifier ("FIRSTNAME"));
MB.SQLBindParameter
(StatementHandle => StatementHandle,
ParameterNumber => 1,
InputOutputType => SQL_PARAM_INPUT,
ValueType => SQL_C_SLONG,
ParameterType => SQL_INTEGER,
ColumnSize => 0,
DecimalDigits => 0,
Value => Search_Manager'Access,
BufferLength => 0,
StrLen_Or_IndPtr => Len'Access);
SQLBindCol
(StatementHandle, 1, Name'Access, Len_Name'Access);
SQLBindCol (StatementHandle, 2, Firstname'Access,
Len_Firstname'Access);
DB.SQLBindCol (StatementHandle, 3, Salary'Access,
Len_Salary'Access);
SQLExecute (StatementHandle);
begin
loop
SQLFetch (StatementHandle);
SQLFixNTS (Name, Len_Name);
SQLFixNTS (Firstname, Len_Firstname);
Put (Name);
Put (", ");
Put (Firstname);
Put (", ");
RIO.Put (Item => Salary, Fore => 5, Aft => 2, Exp => 0);
New_Line;
end loop;
exception
when No_Data =>
null;
end;
end;
SQLCommit (ConnectionHandle);
SQLDisconnect (ConnectionHandle);
SQLFreeHandle (SQL_HANDLE_DBC, ConnectionHandle);
SQLFreeHandle (SQL_HANDLE_ENV, EnvironmentHandle);
end odbc_mysql;
======================================================
Thanks,
Hubert Walter
next reply other threads:[~2008-08-01 12:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-01 12:18 azdakiel [this message]
2008-08-01 13:10 ` Gnade/ODBC example - please help Dmitry A. Kazakov
2008-08-01 13:37 ` Ludovic Brenta
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox