testsqldb.pp 1.9 KB

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