testdb.pp 2.5 KB

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