testdb4.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. program qtest;
  2. uses
  3. mysql4;
  4. Const
  5. DataBase : Pchar = 'testdb';
  6. Query : Pchar = 'Select * from FPdev';
  7. var
  8. count,num : longint;
  9. code : integer;
  10. sock : PMYSQL;
  11. qmysql : TMYSQL;
  12. qbuf : string [160];
  13. rowbuf : TMYSQL_ROW;
  14. dummy : string;
  15. recbuf : PMYSQL_RES;
  16. begin
  17. if paramcount=1 then
  18. begin
  19. Dummy:=Paramstr(1)+#0;
  20. DataBase:=@Dummy[1];
  21. end;
  22. Write ('Connecting to MySQL...');
  23. mysql_init(PMySQL(@qmysql));
  24. sock := mysql_real_connect(PMysql(@qmysql),nil,'michael','geen',nil,0,nil,0);
  25. if sock=Nil then
  26. begin
  27. Writeln (stderr,'Couldn''t connect to MySQL.');
  28. Writeln (stderr,mysql_error(@qmysql));
  29. halt(1);
  30. end;
  31. Writeln ('Done.');
  32. Writeln ('Connection data:');
  33. {$ifdef Unix}
  34. writeln ('Mysql_port : ',mysql_port);
  35. writeln ('Mysql_unix_port : ',mysql_unix_port);
  36. {$endif}
  37. writeln ('Host info : ',mysql_get_host_info(sock));
  38. writeln ('Server info : ',mysql_stat(sock));
  39. writeln ('Client info : ',mysql_get_client_info);
  40. Writeln ('Selecting Database ',DataBase,'...');
  41. if mysql_select_db(sock,DataBase) < 0 then
  42. begin
  43. Writeln (stderr,'Couldn''t select database ',Database);
  44. Writeln (stderr,mysql_error(sock));
  45. halt (1);
  46. end;
  47. writeln ('Executing query : ',Query,'...');
  48. if (mysql_query(sock,Query) < 0) then
  49. begin
  50. Writeln (stderr,'Query failed ');
  51. writeln (stderr,mysql_error(sock));
  52. Halt(1);
  53. end;
  54. recbuf := mysql_store_result(sock);
  55. if RecBuf=Nil then
  56. begin
  57. Writeln ('Query returned nil result.');
  58. mysql_close(sock);
  59. halt (1);
  60. end;
  61. Writeln ('Number of records returned : ',mysql_num_rows (recbuf));
  62. Writeln ('Number of fields per record : ',mysql_num_fields(recbuf));
  63. rowbuf := mysql_fetch_row(recbuf);
  64. while (rowbuf <>nil) do
  65. begin
  66. Write ('(Id: ', rowbuf[0]);
  67. Write (', Name: ', rowbuf[1]);
  68. Writeln(', Email : ', rowbuf[2],')');
  69. rowbuf := mysql_fetch_row(recbuf);
  70. end;
  71. Writeln ('Freeing memory occupied by result set...');
  72. mysql_free_result (recbuf);
  73. Writeln ('Closing connection with MySQL.');
  74. mysql_close(sock);
  75. halt(0);
  76. end.
  77. $Log$
  78. Revision 1.1 2004-09-30 19:34:47 michael
  79. + Split everything in version 3 and version 4
  80. Revision 1.4 2004/09/28 19:08:09 michael
  81. + Some compatibility issues fixed
  82. Revision 1.3 2002/09/07 15:42:53 peter
  83. * old logs removed and tabs fixed
  84. Revision 1.2 2002/05/31 11:54:33 marco
  85. * Renamefest for 1.0, many 1.1.x spots patched also.
  86. Revision 1.1 2002/01/29 17:54:54 peter
  87. * splitted to base and extra
  88. }