testodbc.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 StmtHAndle<>0 then
  29. SQLFreeHandle(SQL_HANDLE_STMT,StmtHandle);
  30. If DBHandle<>0 then
  31. SQLFreeHandle(SQL_HANDLE_DBC,DBHandle);
  32. If EnvHandle<>0 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. loadODBC;
  46. EnvHandle:=0;
  47. DBHandle:=0;
  48. StmtHandle:=0;
  49. Res:=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, EnvHandle);
  50. if Res <> SQL_SUCCESS then
  51. DoError('Could allocate ODBC handle',Res);
  52. Res:=SQLSetEnvAttr(EnvHandle,SQL_ATTR_ODBC_VERSION, SQLPOINTER(SQL_OV_ODBC3), 0);
  53. If Not ODBCSuccess(res) then
  54. DoError('Could not set environment',Res);
  55. Res:=SQLAllocHandle(SQL_HANDLE_DBC, envHandle, DBHandle);
  56. If res<>SQL_SUCCESS then
  57. DoError('Could not create database handle',res);
  58. Res:=SQLConnect(DBHandle,PSQLCHAR(DBDSN),SQL_NTS,
  59. PSQLChar(UserName),SQL_NTS,
  60. PSQLCHAR(Password),SQL_NTS);
  61. If Not OdbcSuccess(res) then
  62. DoError('Could not connect to datasource.',Res);
  63. end;
  64. Procedure ExecuteStatement;
  65. Var
  66. Res,ErrCode : LongInt;
  67. begin
  68. Res:=SQLAllocHandle(SQL_HANDLE_STMT,DBHandle,stmtHandle);
  69. If not ODBCSuccess(res) then
  70. DoError('Could not allocate statement handle.',Res);
  71. { Bind result buffers.
  72. Note that for many queries, the result is not known on beforehand,
  73. And must be queried with SQLPrepare, SQLNumResulCols and SQLDescribeCol
  74. before the statement is executed.}
  75. SQLBindCol(stmtHandle,1,SQL_INTEGER,SQLPointer(@ResID),4,@ErrCode);
  76. SQLBindCol(stmtHandle,2,SQL_CHAR,SQLPointer(@ResName),256,@ErrCode);
  77. SQLBindCol(stmtHandle,3,SQL_CHAR,SQLPointer(@ResEmail),256,@ErrCode);
  78. // Now actually do it.
  79. Res:=SQLExecDirect(StmtHandle,Query,SQL_NTS);
  80. if not ODBCSuccess(res) then
  81. DoError('Execute of statement failed.',Res);
  82. end;
  83. Procedure ShowResult;
  84. Var
  85. Count,Res : Longint;
  86. begin
  87. Res:=SQLFetch(StmtHandle);
  88. Count:=0;
  89. While Res<>SQL_NO_DATA do
  90. begin
  91. Inc(Count);
  92. Write('Record: ',Count,' : ');
  93. Writeln(ResId,' ',PChar(@ResName[0]),' ',Pchar(@ResEmail[0]));
  94. Res:=SQLFetch(StmtHandle);
  95. end;
  96. end;
  97. begin
  98. StartSession;
  99. ExecuteStatement;
  100. ShowResult;
  101. FreeHandles;
  102. end.