testdb.pp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. sock := mysql_connect(PMysql(@qmysql),nil,nil,nil);
  23. if sock=Nil then
  24. begin
  25. Writeln (stderr,'Couldn''t connect to MySQL.');
  26. Writeln (stderr,mysql_error(@qmysql));
  27. halt(1);
  28. end;
  29. Writeln ('Done.');
  30. Writeln ('Connection data:');
  31. {$ifdef linux}
  32. writeln ('Mysql_port : ',mysql_port);
  33. writeln ('Mysql_unix_port : ',mysql_unix_port);
  34. {$endif}
  35. writeln ('Host info : ',mysql_get_host_info(sock));
  36. writeln ('Server info : ',mysql_stat(sock));
  37. writeln ('Client info : ',mysql_get_client_info);
  38. Writeln ('Selecting Database ',DataBase,'...');
  39. if mysql_select_db(sock,DataBase) < 0 then
  40. begin
  41. Writeln (stderr,'Couldn''t select database ',Database);
  42. Writeln (stderr,mysql_error(sock));
  43. halt (1);
  44. end;
  45. writeln ('Executing query : ',Query,'...');
  46. if (mysql_query(sock,Query) < 0) then
  47. begin
  48. Writeln (stderr,'Query failed ');
  49. writeln (stderr,mysql_error(sock));
  50. Halt(1);
  51. end;
  52. recbuf := mysql_store_result(sock);
  53. if RecBuf=Nil then
  54. begin
  55. Writeln ('Query returned nil result.');
  56. mysql_close(sock);
  57. halt (1);
  58. end;
  59. Writeln ('Number of records returned : ',mysql_num_rows (recbuf));
  60. Writeln ('Number of fields per record : ',mysql_num_fields(recbuf));
  61. rowbuf := mysql_fetch_row(recbuf);
  62. while (rowbuf <>nil) do
  63. begin
  64. Write ('(Id: ', rowbuf[0]);
  65. Write (', Name: ', rowbuf[1]);
  66. Writeln(', Email : ', rowbuf[2],')');
  67. rowbuf := mysql_fetch_row(recbuf);
  68. end;
  69. Writeln ('Freeing memory occupied by result set...');
  70. mysql_free_result (recbuf);
  71. Writeln ('Closing connection with MySQL.');
  72. mysql_close(sock);
  73. halt(0);
  74. end.