[fpc-pascal] Writing a Console App in FreePascal that connects to MySQL
Terry A. Haimann
terry at haimann.us
Wed Mar 25 19:53:55 CET 2015
I was trying to see how to write a Console Appication in FreePascal
that will connect to MySQL. I know how to do this in c, but thought it
would be interesting to be able to do it in Pascal.
How should FConnection and FTransaction be declared or are the declared
in a library somewhere?
I have the following source code which is based on the web site
http://wiki.freepascal.org/SqlDBHowto, but does not compile:
Program TestMysql;
Uses sqldb, mysql55conn;
Var
MyConnection: TSQLConnector;
MyTransaction: TSQLTransaction;
MyQuery: TSQLQuery;
function GetQuery : TSQLQuery;
var MyQuery : TSQLQuery;
begin
MyQuery := TSQLQuery.Create;
MyQuery.Database := FConnection;
MyQuery.Transaction := FTransaction;
GetQuery := MyQuery;
end;
procedure CreateTransaction;
begin
MyTransaction := TSQLTransaction.Create;
MyTransaction.Database := MyConnection;
end;
Procedure CreateConnection;
Begin
MyConnection := TSQLConnector.Create(nil);
MyConnection.ConnectorType := 'MySQL 5.5';
MyConnection.Hostname := '127.0.0.1';
MyConnection.DatabaseName := 'MyDB';
MyConnection.UserName := 'MyUser';
MyConnection.Password := 'MyPassword';
End;
Begin
CreateConnection;
CreateTransaction;
MyQuery := GetQuery;
MyQuery.SQL.Text := 'select ObjIdx, ObjCon from object' +
'Order By ObjCon, ObjIdx';
MyConnection.Open;
MyQuery.Open;
If MyConnection.Connected Then
Begin
While Not MyQuery.eof Do
Begin
WriteLn(MyQuery.FieldByName('ObjIdx').AsString,
MyQuery.FieldByName('ObjCon').AsString);
MyQuery.Next;
End;
End Else WriteLn('Failure');
MyQuery.Close
MyConnection.Close;
MyQuery.Free
MyConnection.Free;
MyTransaction.Free;
End.
More information about the fpc-pascal
mailing list