testsqldb.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. program testsqldb;
  2. { A very simple example for sqldb, written by Joost van der Sluis (2004)
  3. Usage:
  4. remove the defines for the databases whose clients aren't installed, or
  5. the linking will fail.
  6. The following parameters are used, in given order:
  7. parameter1 = databasetype (mysql,interbase,postgresql - case sensitive)
  8. parameter2 = databasename
  9. parameter3 = tablename
  10. parameter4 = username, optional
  11. parameter5 = password, optional
  12. This example will only display the data for each record in the given table.
  13. Examples:
  14. ./testsqldb postgresql testdb fpdev
  15. ./testsqldb interbase /home/firebird/dbtest.fdb fpdev sysdba 123456
  16. }
  17. {$mode objfpc}{$H+}
  18. {$define pqconnection}
  19. {$define MySQLConnection}
  20. {$define IBConnection}
  21. uses
  22. Classes,
  23. {$ifdef pqconnection} pqconnection, {$endif}
  24. {$ifdef MySQLConnection} mysql4conn, {$endif}
  25. {$ifdef IBConnection} IBConnection, {$endif}
  26. sqldb;
  27. var connection : tSQLConnection;
  28. transaction : tSQLTransaction;
  29. query : tSQLQuery;
  30. tel : integer;
  31. dbtype : string;
  32. begin
  33. dbtype := paramstr(1);
  34. {$ifdef MySQLConnection}
  35. if dbtype = 'mysql' then connection := tMySQLConnection.Create(nil);
  36. {$endif}
  37. {$ifdef pqconnection}
  38. if dbtype = 'postgresql' then connection := tpqConnection.Create(nil);
  39. {$endif}
  40. {$ifdef IBConnection}
  41. if dbtype = 'interbase' then connection := tIBConnection.Create(nil);
  42. {$endif}
  43. if not assigned(connection) then exit; // probably an invalid database type given
  44. connection.DatabaseName := paramstr(2);
  45. connection.UserName := paramstr(4);
  46. connection.Password := paramstr(5);
  47. connection.open;
  48. transaction := tsqltransaction.create(nil);
  49. transaction.database := connection;
  50. query := tsqlquery.Create(nil);
  51. query.DataBase := connection;
  52. query.transaction := transaction;
  53. with query do
  54. begin
  55. SQL.clear;
  56. sql.add('select * from ' + paramstr(3));
  57. open;
  58. while not eof do
  59. begin
  60. for tel := 0 to query.FieldCount -1 do
  61. write(fields[tel].asstring+' ');
  62. writeln;
  63. next;
  64. end;
  65. close;
  66. query.free;
  67. end;
  68. transaction.free;
  69. connection.free;
  70. end.