9
Query : pchar = 'SELECT Id,Username,InstEmail from FPdev Order by UserName';
12
UserName : pchar = 'michael'; // for mysql test.
13
Password : pchar = 'geen';
15
UserName : pchar = ''; // for MS-Acces test.
16
Password : pchar = '';
19
Function ODBCSuccess (Res : Integer) : Boolean;
22
ODBCSuccess:= (res=SQL_SUCCESS) or (res=SQL_SUCCESS_WITH_INFO);
26
EnvHandle : SQLHandle;
28
StmtHandle : SQLHSTMT;
30
ResName : Array[0..255] of char; // Matches length of field+1
31
ResEmail : Array[0..255] of char;
33
Procedure FreeHandles;
36
If assigned(StmtHAndle) then
37
SQLFreeHandle(SQL_HANDLE_STMT,StmtHandle);
38
If assigned(dbhandle) then
39
SQLFreeHandle(SQL_HANDLE_DBC,DBHandle);
40
If assigned(EnvHandle) then
41
SQLFreeHandle(SQL_HANDLE_ENV,EnvHandle);
44
Procedure DoError (Msg : String;ErrCode : Integer);
48
Writeln(Msg,' Code : ',ErrCode);
52
Procedure StartSession;
61
Res:=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, EnvHandle);
62
if Res <> SQL_SUCCESS then
63
DoError('Could allocate ODBC handle',Res);
64
Res:=SQLSetEnvAttr(EnvHandle,SQL_ATTR_ODBC_VERSION, SQLPOINTER(SQL_OV_ODBC3), 0);
65
If Not ODBCSuccess(res) then
66
DoError('Could not set environment',Res);
67
Res:=SQLAllocHandle(SQL_HANDLE_DBC, envHandle, DBHandle);
68
If res<>SQL_SUCCESS then
69
DoError('Could not create database handle',res);
70
Res:=SQLConnect(DBHandle,PSQLCHAR(DBDSN),SQL_NTS,
71
PSQLChar(UserName),SQL_NTS,
72
PSQLCHAR(Password),SQL_NTS);
73
If Not OdbcSuccess(res) then
74
DoError('Could not connect to datasource.',Res);
77
Procedure ExecuteStatement;
80
Res,ErrCode : LongInt;
83
Res:=SQLAllocHandle(SQL_HANDLE_STMT,DBHandle,stmtHandle);
84
If not ODBCSuccess(res) then
85
DoError('Could not allocate statement handle.',Res);
86
{ Bind result buffers.
87
Note that for many queries, the result is not known on beforehand,
88
And must be queried with SQLPrepare, SQLNumResulCols and SQLDescribeCol
89
before the statement is executed.}
90
SQLBindCol(stmtHandle,1,SQL_INTEGER,SQLPointer(@ResID),4,@ErrCode);
91
SQLBindCol(stmtHandle,2,SQL_CHAR,SQLPointer(@ResName),256,@ErrCode);
92
SQLBindCol(stmtHandle,3,SQL_CHAR,SQLPointer(@ResEmail),256,@ErrCode);
93
// Now actually do it.
94
Res:=SQLExecDirect(StmtHandle,Query,SQL_NTS);
95
if not ODBCSuccess(res) then
96
DoError('Execute of statement failed.',Res);
105
Res:=SQLFetch(StmtHandle);
107
While Res<>SQL_NO_DATA do
110
Write('Record: ',Count,' : ');
111
Writeln(ResId,' ',PChar(@ResName[0]),' ',Pchar(@ResEmail[0]));
112
Res:=SQLFetch(StmtHandle);