2
0

testodbc.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Program TestODBC;
  2. uses odbcsql;
  3. Const
  4. DBDSn : Pchar = 'FPC';
  5. Empty : pchar = '';
  6. Query : pchar = 'SELECT Id,Username,InstEmail from FPdev Order by UserName';
  7. // Adapt to needs...
  8. {$ifdef linux}
  9. UserName : pchar = 'michael'; // for mysql test.
  10. Password : pchar = 'geen';
  11. {$else}
  12. UserName : pchar = ''; // for MS-Acces test.
  13. Password : pchar = '';
  14. {$endif}
  15. Function ODBCSuccess (Res : Integer) : Boolean;
  16. begin
  17. ODBCSuccess:= (res=SQL_SUCCESS) or (res=SQL_SUCCESS_WITH_INFO);
  18. end;
  19. Var
  20. EnvHandle : SQLHandle;
  21. DBHandle : SQLHandle;
  22. StmtHandle : SQLHSTMT;
  23. ResID : Longint;
  24. ResName : Array[0..255] of char; // Matches length of field+1
  25. ResEmail : Array[0..255] of char;
  26. Procedure FreeHandles;
  27. begin
  28. If assigned(StmtHAndle) then
  29. SQLFreeHandle(SQL_HANDLE_STMT,StmtHandle);
  30. If assigned(dbhandle) then
  31. SQLFreeHandle(SQL_HANDLE_DBC,DBHandle);
  32. If assigned(EnvHandle) then
  33. SQLFreeHandle(SQL_HANDLE_ENV,EnvHandle);
  34. end;
  35. Procedure DoError (Msg : String;ErrCode : Integer);
  36. begin
  37. FreeHandles;
  38. Writeln(Msg,' Code : ',ErrCode);
  39. Halt(1);
  40. end;
  41. Procedure StartSession;
  42. Var
  43. Res : Integer;
  44. begin
  45. EnvHandle:=nil;
  46. DBHandle:=nil;
  47. StmtHandle:=nil;
  48. Res:=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, EnvHandle);
  49. if Res <> SQL_SUCCESS then
  50. DoError('Could allocate ODBC handle',Res);
  51. Res:=SQLSetEnvAttr(EnvHandle,SQL_ATTR_ODBC_VERSION, SQLPOINTER(SQL_OV_ODBC3), 0);
  52. If Not ODBCSuccess(res) then
  53. DoError('Could not set environment',Res);
  54. Res:=SQLAllocHandle(SQL_HANDLE_DBC, envHandle, DBHandle);
  55. If res<>SQL_SUCCESS then
  56. DoError('Could not create database handle',res);
  57. Res:=SQLConnect(DBHandle,PSQLCHAR(DBDSN),SQL_NTS,
  58. PSQLChar(UserName),SQL_NTS,
  59. PSQLCHAR(Password),SQL_NTS);
  60. If Not OdbcSuccess(res) then
  61. DoError('Could not connect to datasource.',Res);
  62. end;
  63. Procedure ExecuteStatement;
  64. Var
  65. Res,ErrCode : LongInt;
  66. begin
  67. Res:=SQLAllocHandle(SQL_HANDLE_STMT,DBHandle,stmtHandle);
  68. If not ODBCSuccess(res) then
  69. DoError('Could not allocate statement handle.',Res);
  70. { Bind result buffers.
  71. Note that for many queries, the result is not known on beforehand,
  72. And must be queried with SQLPrepare, SQLNumResulCols and SQLDescribeCol
  73. before the statement is executed.}
  74. SQLBindCol(stmtHandle,1,SQL_INTEGER,SQLPointer(@ResID),4,@ErrCode);
  75. SQLBindCol(stmtHandle,2,SQL_CHAR,SQLPointer(@ResName),256,@ErrCode);
  76. SQLBindCol(stmtHandle,3,SQL_CHAR,SQLPointer(@ResEmail),256,@ErrCode);
  77. // Now actually do it.
  78. Res:=SQLExecDirect(StmtHandle,Query,SQL_NTS);
  79. if not ODBCSuccess(res) then
  80. DoError('Execute of statement failed.',Res);
  81. end;
  82. Procedure ShowResult;
  83. Var
  84. Count,Res : Longint;
  85. begin
  86. Res:=SQLFetch(StmtHandle);
  87. Count:=0;
  88. While Res<>SQL_NO_DATA do
  89. begin
  90. Inc(Count);
  91. Write('Record: ',Count,' : ');
  92. Writeln(ResId,' ',PChar(@ResName[0]),' ',Pchar(@ResEmail[0]));
  93. Res:=SQLFetch(StmtHandle);
  94. end;
  95. end;
  96. begin
  97. StartSession;
  98. ExecuteStatement;
  99. ShowResult;
  100. FreeHandles;
  101. end.