testsqldb.pp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. mysql51conn,
  19. IBConnection,
  20. sqlite3conn,
  21. sqldb;
  22. var connection : tSQLConnection;
  23. transaction : tSQLTransaction;
  24. query : tSQLQuery;
  25. tel : integer;
  26. dbtype : string;
  27. begin
  28. dbtype := paramstr(1);
  29. if dbtype = 'mysql' then connection := tMySQL51Connection.Create(nil);
  30. if dbtype = 'postgresql' then connection := tpqConnection.Create(nil);
  31. if dbtype = 'interbase' then connection := tIBConnection.Create(nil);
  32. if dbtype = 'sqlite3' then connection := tSQLite3Connection.Create(nil);
  33. if not assigned(connection) then exit; // probably an invalid database type given
  34. connection.DatabaseName := paramstr(2);
  35. connection.UserName := paramstr(4);
  36. connection.Password := paramstr(5);
  37. connection.open;
  38. transaction := tsqltransaction.create(nil);
  39. transaction.database := connection;
  40. query := tsqlquery.Create(nil);
  41. query.DataBase := connection;
  42. query.transaction := transaction;
  43. with query do
  44. begin
  45. SQL.clear;
  46. sql.add('select * from ' + paramstr(3));
  47. ReadOnly := True; // If the query is writeable, a transaction must be assigned
  48. // to the database.
  49. open;
  50. while not eof do
  51. begin
  52. for tel := 0 to query.FieldCount -1 do
  53. write(fields[tel].asstring+' ');
  54. writeln;
  55. next;
  56. end;
  57. close;
  58. query.free;
  59. end;
  60. transaction.free;
  61. connection.free;
  62. end.