constructor TPDO.create;
Instanciates the PDO object. Normally it would be desirable for the database connection to persist the entire time the program is running. In that case, a permanent global variable established at initialization of a unit is desirable. In fact, I like to make a global statement and fetch structures for reuse, like this:
unit dbUnit;
interface
{$I PDO/PDOjrm.inc}
{these driver units aren't needed directly, but I put them here to force the libraries
to open BEFORE the db variable is initialized. Otherwise the libraries are
removed before the db variable is freed, causing a runtime 216 error}
uses PDO, PDOStatement, myLogListener, sysUtils
{$IFDEF MYSQL41DRIVER}
, PDOPlainMySql41
{$ENDIF}
{$IFDEF MYSQL50DRIVER}
, PDOPlainMySql50
{$ENDIF}
;
var
db: TPDO;
row: TPDORowNative;
rowset: TPDORowSetNative;
rowKeyPair: TPDORowKeyStrings;
rowsetKeyPair: TPDORowSetKeyStrings;
DatabaseLogListener: TMavLogListener;
stmt: IPDOStatement;
selectSQL: PDO_selectRecord;
implementation
initialization
DatabaseLogListener := TMavLogListener.Create;
db := TPDO.Create;
row := TPDORowNative.Create;
rowset := TPDORowSetNative.Create;
rowKeyPair := TPDORowKeyStrings.Create;
rowsetKeyPair := TPDORowSetKeyStrings.Create;
finalization
rowsetKeyPair.Free;
rowKeyPair.Free;
rowset.Free;
row.Free;
db.Free;
DatabaseLogListener.Free;
end.
Obviously you can create and free database objects as often as you like.